【Leetcode长征系列】Palindrome Number

原型:

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.

思路:

如果它的input是一个string类型,我们就可以利用string的函数对首尾元素进行分析。只可惜它要求我们不能用多余空间,并且input还是一个int类型。

所以我的想法是,用一个int n 去记住我们这个数字的长度。然后每次都把最高位和最低位单独取出来比较。

代码:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        if(x/10==0) return true;
        
        int rem = x;
        int n = 0;  //to keep the value of how long the number is.
        while(x/10!=0){
            n++;
            x=x/10;
        }
        n++;
        
        int head = rem, tail = 0;
        for(int i = n-1; i>0 && n>0; n-=2, i = n-1){
            tail = rem%10;
            while(i>0){
                head = head/10;
                i--;
            }
            if (head!=tail) return false;
            rem -=head*pow(head,10);
            rem = rem % 10;
        }
        return true;
    }
};
不过卡在了一个很奇怪的数字上:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        if(x/10==0) return true;
        
        int rem = x;
        int n = 0;  //to keep the value of how long the number is.
        while(x/10!=0){
            n++;
            x=x/10;
        }
        n++;
        
        int head = rem, tail = 0;
        for(int i = n-1; i>0 && n>0; n-=2, i = n-1){
            tail = rem%10;
            while(i>0){
                head = head/10;
                i--;
            }
            if (head!=tail) return false;
            rem = rem / 10;
            rem = rem - head*pow(n-2,10);
            head = rem;
        }
        return true;
    }
};

哭…

=================================================================================================================================

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (x < 0)
            return false;
        if (x == 0)
            return true;
            
        int base = 1;
        while(x / base >= 10)
            base *= 10;
            
        while(x)
        {
            int leftDigit = x / base;
            int rightDigit = x % 10;
            if (leftDigit != rightDigit)
                return false;
            
            x -= base * leftDigit;
            base /= 100;
            x /= 10;
        }
        
        return true;
    }
};
这是网上的代码,明明思路一样啊摔!!!!!!

但是总结一下。虽然很多题每次看着都是很有思路的,而且大部分时候思路都是完全正确的,但依然写不好代码。主要是找不到那个最简化或者说最适合用来写代码的点。比如这道题,网络代码直接用了一个变量base来记录位数,而我偏偏把这个base量化成了n位,在后面的计算中其实很不方便,也很容易出错。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值