代码改变世界

leetcode - Palindrome Number

2013-11-02 21:49  张汉生  阅读(191)  评论(0编辑  收藏  举报

 

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