Determine whether an integer is a palindrome(回文). Do this without extra space.

 1 bool isPalindrome(int x) {
 2     if(x<0) return false;
 3     
 4     int x1 = x;
 5     int mirror = 0;
 6     while(x1){
 7         mirror = mirror*10 + x1%10;
 8         x1 = x1/10;
 9     }
10     return mirror == x;
11 
12 }

 

 1 class Solution(object):
 2     def isPalindrome(self, x):
 3         """
 4         :type x: int
 5         :rtype: bool
 6         """
 7         if x<0:
 8             return False
 9         
10         s = str(x)
11         return s[::-1] == s

 

posted on 2017-02-28 19:40  Ci_pea  阅读(131)  评论(0编辑  收藏  举报