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

 

思路:

1.利用leetcode #7 的reverse(int x)

2.详见code

 

代码:思路2 C++

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

 参考:

https://leetcode.com/discuss/33500/an-easy-lines-code-only-reversing-till-half-and-then-compare

posted on 2016-03-15 20:41  gavinXing  阅读(101)  评论(0编辑  收藏  举报