https://leetcode.com/problems/palindrome-number/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Solution { public boolean isPalindrome(int x) { if(x == 0) return true; if(x < 0) return false; long rev = 0; int val = x; int count = 0; while( val!= 0){ rev = rev * 10 + val % 10; val /= 10; count++; if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) return false; } int irev = (int)rev; while(x != irev){ int shift = count * 10; int left = irev / shift; int right = x % 10; if( left != right) return false; x/= 10; irev /= shift; count--; } return true; } }
|