码迷,mamicode.com
首页 > 其他好文 > 详细

Palindrome Number

时间:2015-01-29 23:57:41      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

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;
       
     
    }
}

 

Palindrome Number

标签:

原文地址:http://www.cnblogs.com/mrpod2g/p/4261166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!