思路1:利用Reverse Integer的方法,求的转换后的数字,然后比较是否相等。

 

思路2:从两头依次取数字比较,向中间推进。

 1     // Revert Integer解法,把反转后的和原数据对比,相同返回true
 2     public static boolean isPalindrome(int x) {
 3         int real = x;
 4         if (x < 0) {
 5             return false;
 6         }
 7         long sum = 0;
 8         while (x > 0) {
 9             int temp = x % 10;
10             sum = sum * 10 + temp;
11             if (sum > Integer.MAX_VALUE) {
12                 return false;
13             }
14             x = x / 10;
15         }
16         if (sum == real) {
17             return true;
18         } else {
19             return false;
20         }
21     }
22 
23     // left和right比较,例如1234321:左一和右一比较,左二和右二比较,如果全部相等返回true
24     public static boolean isPalindrome_2(int x) {
25         if (x < 0) {
26             return false;
27         }
28         int len = 1;
29         while (x / len >= 10) {
30             len = len * 10;
31         }
32         while (x > 0) {
33             int left = x / len;
34             int right = x % 10;
35             if (left != right) {
36                 return false;
37             }
38             x = (x - left * len) / 10;
39             len = len / 100;
40         }
41         return true;
42     }

 

posted on 2016-01-25 16:03  酒仙桥吴彦祖  阅读(124)  评论(0编辑  收藏  举报