Palindrome Number

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

 

public class Solution {
    public boolean isPalindrome(int x) {
    	if (x < 0) {
    		return false;
    	}
        int digits = 0;
        int quotient = x;
        while (quotient != 0) {
        	quotient /= 10;
        	digits++;
        }
        for (int i = 1; i <= digits; i++) {
        	int low = i;
        	int high = digits-i+1;
        	if (getDigit(x, low) != getDigit(x, high)) {
        		return false;
        	}
        }
        return true;
    }
    public int getDigit(int x, int i) {
    	if (i == 1) {
    		return x%10;
    	} else {
    		return (int) ((x/Math.pow(10, i-1))%10);
    	}
    } 
}

 

你可能感兴趣的:(java)