author: Blankj blog : http://blankj.com time : 2017/04/24 desc :
| 9 | * </pre> |
| 10 | */ |
| 11 | public class Solution { |
| 12 | // public boolean isPalindrome(int x) { |
| 13 | // if (x < 0) return false; |
| 14 | // int copyX = x, reverse = 0; |
| 15 | // while (copyX > 0) { |
| 16 | // reverse = reverse * 10 + copyX % 10; |
| 17 | // copyX /= 10; |
| 18 | // } |
| 19 | // return x == reverse; |
| 20 | // } |
| 21 | |
| 22 | public boolean isPalindrome(int x) { |
| 23 | if (x < 0 || (x != 0 && x % 10 == 0)) return false; |
| 24 | int halfReverseX = 0; |
| 25 | while (x > halfReverseX) { |
| 26 | halfReverseX = halfReverseX * 10 + x % 10; |
| 27 | x /= 10; |
| 28 | } |
| 29 | return halfReverseX == x || halfReverseX / 10 == x; |
| 30 | } |
| 31 | |
| 32 | public static void main(String[] args) { |
| 33 | Solution solution = new Solution(); |
| 34 | System.out.println(solution.isPalindrome(-1)); |
| 35 | System.out.println(solution.isPalindrome(10010)); |
| 36 | |
| 37 | System.out.println(solution.isPalindrome(0)); |
| 38 | System.out.println(solution.isPalindrome(11)); |
| 39 | System.out.println(solution.isPalindrome(111)); |
| 40 | System.out.println(solution.isPalindrome(222222222)); |
| 41 | } |
| 42 | } |
nothing calls this directly
no outgoing calls
no test coverage detected