9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
判断一个数是不是回文数
C++(182ms):
1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if(x < 0 || (x != 0 && x%10 == 0)) 5 return false ; 6 int sum = 0 ; 7 while(x > sum){ 8 sum = sum*10 + x%10 ; 9 x /= 10 ; 10 } 11 return (x == sum) || (x == sum/10) ; 12 } 13 };
Java(188ms):
1 class Solution { 2 public boolean isPalindrome(int x) { 3 if(x < 0 || (x != 0 && x%10 == 0)) 4 return false ; 5 int sum = 0 ; 6 while(x > sum){ 7 sum = sum*10 + x%10 ; 8 x /= 10 ; 9 } 10 return (x == sum) || (x == sum/10) ; 11 } 12 }