Palindrome Number

来源:互联网 发布:数据时代的利与弊作文 编辑:程序博客网 时间:2024/05/18 19:22

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

方法:数字前后位分别提取并进行判断是否相等。

class Solution {public:    bool isPalindrome(int x) {        if(x<0)            return false;        int y = x;        int len = 1;        while(y/10!=0){            len++;            y/=10;        }        bool isValid ;        int left = len-1 ,right = 1;        while(left >= right){            int big = x /static_cast<int>(pow(10,left)) % 10;            int small = x % static_cast<int>((pow(10,right)))/pow(10,right-1);            if(big != small)                return false;            --left;            ++right;        }        return true;    }};
0 0
原创粉丝点击