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

leetcode题解||Palindrome Number问题

时间:2015-03-17 22:00:59      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:回文数   int反转   

problem:

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

click to show spoilers.

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.

thinking:


(1)之前求过int型逆序反转,这里检验一个数是否为一个回文数,可以得到一点启发。

(2)逆序反转主要涉及溢出问题的处理,而且反转的空间复杂度可以忽略不计,满足题目要求。

code:

class Solution {
public:
    bool isPalindrome(int x) {
        int b=0;
        int a=x;
        bool flag=true;
        if(a<0)
        {
            bool flag=false;
            a=-x;
        }
        while(a)
        {
            b*=10;
            b+=a%10;
            a=a/10;
        }
        if(b>2147483647)
            return false;
        if(!flag)
            b=-b;
        return (b==x);
           
        
    }
};


leetcode题解||Palindrome Number问题

标签:回文数   int反转   

原文地址:http://blog.csdn.net/hustyangju/article/details/44347011

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