leetcode_回文数

本题比较简单,

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x < 0) return false;
 5         int a[100];
 6         int i = 0;
 7         while(x != 0) {
 8             a[i] = x % 10;
 9             x /= 10;
10             i++;
11         }    //此时i表示的是总共有多少个数字
12         for(int j = 0; j < i/2; j++) {
13             if(a[j] != a[i - 1 -j]) return false; 
14             //而这里表示的是数组的下标,所以要减一
15         }
16         return true;
17     }
18 };

2、反转比较

思路其实也比较简单,就是整体比较

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if (x < 0) return false;
 5         long long rev = 0,ori=x;
 6         while (x != 0) {
 7             rev *= 10;
 8             rev += x % 10;
 9             x /= 10;
10         }
11         if (ori == rev) return true;
12         else return false;
13     }
14 };

借用了leetcode 7 整数反转的思路

原文地址:https://www.cnblogs.com/huangming-zzz/p/10212514.html