【Palindrome Number】 --MyLeetCode(四)

来源:互联网 发布:php支持mysql 编辑:程序博客网 时间:2024/06/05 01:52

Description:


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


palindrome: 7、666、12321、2332...


My Solution:

class Solution {public:    bool isPalindrome(int x) {        int res = 0;        int y = x;        if(x < 0) return false;        while(y){            res = res * 10 + y % 10;            y /= 10;        }        return (x == res);    }};



Brilliant solution:

class Solution {public:    bool isPalindrome(int x) {        if(x<0|| (x!=0 &&x%10==0)) return false;        int sum=0;        while(x>sum)        {            sum = sum*10+x%10;            x = x/10;        }        return (x==sum)||(x==sum/10);    }};


原创粉丝点击