zzndb's world
All things will better soon...let's see...
No.6 AC
一道简单题,记的流水帐

依旧easy难度

Description: Determine whether an integer is a palindrome. Do this without extra space. 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.

**tip:**好吧上面粗体问句答案是「负数并不是回文数」

Code:

class Solution {
public:
    bool isPalindrome(int x) {
        int tmp = x, re = 0;
        if(x >= 0){
            while(x){
              re = re*10 + x%10;
              x /= 10;
          }
            if(re == tmp) return 1;
            else return 0;
        }else return 0; 
    }
};

by the way 用到了上一道题的 reverse 然后与原来数比较,排除负数。

FullWeb: 顺便看了看detail,真可怕

好吧,又看了看其他人的,恩很溜,成功浓缩到了八行,but其实没啥区别:stuck_out_tongue::

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);
    }
};

好吧

以上


Last modified on 2017-07-22