# LeetCode 9：Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */class Solution {    public boolean isPalindrome(int x) {        String s = Integer.toString(x);        int left = 0;        int right = s.length() - 1;        while (left < s.length() - 1 && right > 0 && left <= right && s.charAt(left) == s.charAt(right)) {            left++;            right--;        }                return s.charAt(left) == s.charAt(right);    }}`

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */class Solution {    public boolean isPalindrome(int x) {        int a = 0;        int y = x;        // 123  = (1 * 10 + 2) * 10 + 3        // 3    = 123 % 10        // 32   = 3 * 10 + 12 % 10        // 321  = 32 * 10 + 1 % 10        while (y > 0) {            a = a * 10 + y % 10;            y /= 10;        }                return x == y;    }}`