部分文章内容为公开资料查询整理,原文出处可能未标注,如有侵权,请联系我,谢谢。邮箱地址:gnivor@163.com ►►►需要气球么?请点击我吧!

LeetCode--9. Palindrome Number

判断一个数是否是回文数,注意只能用O(1)的空间。

因此不能转换成字符串,也不能把数字解析成数组,只能一个个进行比较。

这里设置一个base,用来每次取第一个数。

public class Solution {
    public static void main(String[] args) {
        System.out.println(isPalindrome(10501));
    }

    public static boolean isPalindrome(int x) {
        if(x<0) 
            return false;
        int base = 1;
        while(x/base>=10){
            base = base*10;
        }
        //System.out.println(base);
        while(x!=0){
            //System.out.println(x/base+" "+x%10);
            if(x/base==x%10){
                x=x%base/10;
                base=base/100;
            }else
                return false;
        }
        return true;
    } x
}

 

posted @ 2015-07-14 16:19  流了个火  阅读(105)  评论(0)    收藏  举报
►►►需要气球么?请点击我吧!►►►
View My Stats