9. Palindrome Number

来源:互联网 发布:jamp软件的作用 编辑:程序博客网 时间:2024/06/05 22:41

题目描述

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.

确定一个整数是否是回文数,负数直接不是回文数。

思路

1. 错误思路

用栈来实现,首先确定数的位数是偶数还是奇数。

1123 是四位,那么数的位数是偶数。

11235 是五位,那么数的位数是奇数。

  1. 偶数的话从右到左不相同的就入栈,新的数中个位的值与栈顶判断,相同则将栈中元素pop出去。最后判断整个栈的状态,栈空表示是回文数,不空表示不是回文数。

  2. 奇数的话,从右往左扫描,中间那个位数不管它,其他原理与偶数的相同。

#include<iostream>#include<algorithm>#include<stack>using namespace std;stack<int> q;class Solution {public:    bool isPalindrome(int x)     {        count = 0;        if(x < 0)            return false;        if(x < 10)            return true;        z = x;        while(z > 0)        {            z = z / 10;            count++;        }        //cout<<count<<endl;        if(count % 2 == 0)        {            while(x)            {                t = x % 10;                x = x / 10;                if(!q.empty() && t == q.top())                {                    q.pop();                }                else if(q.empty() || t != q.top())                q.push(t);            }            if(q.empty())                return true;            else                 return false;        }        else if(count % 2 == 1)        {            i = 0;            while(x)            {                i++;                t = x % 10;                x = x / 10;                if((i != count / 2 + 1) && !q.empty() && t == q.top())                {                    q.pop();                }                else if((i != count / 2 + 1)&&(q.empty() || t != q.top()))                q.push(t);            }            if(q.empty())                return true;            else                 return false;        }    }public:    int z, count, t, i;};int main(){    Solution a;    int x;    cin>>x;    printf("%d",a.isPalindrome(x));    return 0;}

最后发现1122时过不了,因为都被消掉了,思路错误。

2. 正确思路

数的首位与末位判断,然后各自向中间推进,一次不相同就不是回文数,都相等时退出循环,就是回文数。

class Solution {public:    bool isPalindrome(int x)     {        int left, right;        int i, j;        int count = 0, t;        if(x < 0)            return false;        if(x < 10)            return true;        int z = x;        while(z > 0)        {            z = z / 10;            count++;        }        //cout<<count<<endl;        i = 0;        j = count-1;        while(i <= j)        {                left = x / int(pow(10,j)) % 10;                right = x / int(pow(10,i)) % 10;                //cout<<left<<' '<<right<<' '<<endl;                i++;                j--;                if(left != right)                {                    return false;                }        }        return true;    }};
原创粉丝点击