Monday, July 7, 2014

Leetcode - Palindrome Number

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

[Thoughts]:
public class Solution {
    public boolean isPalindrome(int x) {
        if(x<0) return false;
        int div=1; // div是和x相同位数的最小数,如x=110, div=100
        while(x/div>=10)
            div = div*10;
        
        while(x>0){
            int high = x/div;
            int low = x%10;
            
            if(high!=low)
                return false;
                
            x = (x%div)/10;//x%div 去掉了x的首位,除以10去掉尾位
            div = div/100;//去掉首尾两位,div应为100
        }
        return true;
    }
}

No comments:

Post a Comment