Palindrome Number

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

 

方法一:

public class Solution {
    public boolean isPalindrome(int x) {
        String str = x+"";
        char[] charArray = str.toCharArray();
        int size = charArray.length;
        for(int i=0;i<size/2;i++){
            if(charArray[i]!=charArray[size-1-i]){
                return false;
            }
        }
        return true;
    }
}

这里用了一个额外的数组charArray,但最后也Accept了。

 

方法二:

public class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        }
        
        if(x==0){
            return true;
        }
        
        int e = 1;
        while(x/e>=10){
            e = e*10;
        }
        
        int highDigit,lowDigit;
        
        while(x!=0){
            highDigit = x/e;
            lowDigit = x%10;
            if(highDigit!=lowDigit){
                return false; 
            }
            
            x = x-highDigit*e;
            x = x/10;
            e = e/100;
        }
        
        return true;
       
     
    }
}

 

posted @ 2015-01-29 22:19  mrpod2g  阅读(123)  评论(0编辑  收藏  举报