Palindrome Number

 1     bool isPalindrome(int x) {
 2         if(x<0)
 3             return false;
 4         string s=x+" ";
 5         int i,j;
 6         int n=s.length();
 7         for(i=0,j=n-1;i<j;++i,--j)
 8         {
 9             if(s[i]!=s[j])
10             {
11                 return false;
12             }
13         }
14         return true;
15     }

strlen 和 length  :http://stackoverflow.com/questions/905355/c-string-length

代码不正确,第四行,忘记在哪里学的把整型变为字符串 直接+” “(Java中学的还是自创的?)反正在C++中不能这样啊

C++ int to string :http://www.cppblog.com/forLinda/archive/2011/12/29/4298.html

我用itoa报错,所以用了stringstream

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x<0)  //0是回文
 5             return false;
 6             
 7         stringstream ss;
 8         ss<<x;
 9         string s=ss.str();
10       
11         int i,j;
12         int n=s.length();
13         for(i=0,j=n-1;i<j;++i,--j)
14         {
15             if(s[i]!=s[j])
16             {
17                 return false;
18             }
19         }
20         return true;
21     }
22 };

 觉得这个写的更好:http://blog.csdn.net/zzran/article/details/8523869

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x<0)
 5             return false;
 6         int div=1;
 7         int i,j;
 8         while(x/div>=10)
 9             div*=10;
10         while(x)
11         {
12             i=x/div;
13             j=x%10;
14             if(i!=j)
15                 return false;
16             x=(x%div)/10;
17             div /= 100;
18         }
19         return true;
20     }
21 };
原文地址:https://www.cnblogs.com/crane-practice/p/3596723.html