关闭

leetcode 9. Palindrome Number

标签: leetcode
74人阅读 评论(0) 收藏 举报
分类:

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

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

判断一个整数是否是回文数,负数返回false.

public class A9PalindromeNumber {
	public boolean isPalindrome(int x) {
        if(x < 0) return false;
        int m = x;
        long y = 0;
        while(m > 0) {
        	y = y * 10 + m % 10;
        	m /= 10;
        	if(y > Integer.MAX_VALUE)
        		return false;
        }
		return y == x ? true : false;
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22763次
    • 积分:1128
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:11篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论