LeetCode9. 回文数


题目

分析

代码

LeetCode7.整数反转 的变形

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x < 0) return false;
 5         int n = 0,o = x;
 6         while(x){
 7             if(n >= (INT_MAX - x % 10) / 10) return false;
 8             n = n*10 + x % 10;
 9             x /= 10;
10             
11         }
12         if(n == o ) 
13             return true;
14         else
15             return false;
16     }
17 };

将整形编程字符串,然后字符串反转

1 class Solution {
2 public:
3     bool isPalindrome(int x) {
4         if(x < 0) return false;
5         string s = to_string(x);
6         return s == string(s.rbegin(),s.rend());
7     }
8 };

这里使用了反向迭代器 rbegin(), rend() 见  https://blog.csdn.net/xingyanxiao/article/details/45317409

相关