首页 > 其他 > 详细

[leetcode] Palindrome Number

时间:2015-08-19 10:56:44      阅读:220      评论:0      收藏:0      [点我收藏+]

题目:

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

 

解题思路:

循环取得首位和末尾,然后比较。

 

解题心得:

关于整数的处理有几个 运算需要熟记:

一个整数:

 %10 得个位 , %100 的后两位 就是个位和十位 %1000...依次类推

 /10 去个位 得从十位开始的数 , /100 去十位 个位, 得从百位开始的数... 依次类推;

 

 

 

public class Solution {
    public boolean isPalindrome(int x) {
        if(x<0) 
           return false;
        
        int div=1;
        while(x/div>=10)
            div*=10;
        
        while(x!=0)
        {
            int last =x%10 ;
            int first = x/div;
            if(first!=last) return false;
            
            x =(x%div)/10;
            div/=100;
        }
        
        return true;
    }
}

 

[leetcode] Palindrome Number

原文:http://www.cnblogs.com/fengmangZoo/p/4741346.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!