9 Palindrome Number

Question:

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

Analysis

Use base

Without extra space means no more than O(1).

Code:

public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        int length = 0;
        int value = x;
        int base = 1;
        while (value / 10 != 0) {
            base *= 10;
            value /= 10;
        }
        while (x > 0 && base >= 10) {
            if (x % 10 != x / base) {
                return false;
            } else {
                x = x % base;
                x = x / 10;
            }
            base /= 100;
        }
        return true;
    }
}