JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

注意负数

-2147483648的绝对值还是自己!!!(overflow)

 1 public class Solution {
 2     public boolean isPalindrome(int x) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if( x<0 )
 6             return false;
 7         int tmp = x;
 8         int mask = 1;
 9         tmp /= 10;
10         while(tmp > 0)
11         {
12             mask *= 10;
13             tmp /= 10;
14         }
15         
16         while(x > 0)
17         {
18             if(x / mask == x % 10)
19             {
20                 x %= mask;
21                 x /= 10;
22                 mask /= 100;
23             }
24             else
25                 return false;
26         }
27         return true;
28         
29     }
30 }

 

 

posted on 2013-11-05 08:46  JasonChang  阅读(163)  评论(0编辑  收藏  举报