LeetCode Day3 Palindrome Number

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.
For example, 121 is palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left.
Therefore it is not a palindrome.

Example 4:

Input: x = -101
Output: false

Constraints:

-231 <= x <= 231 - 1

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/palindrome-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这个题做的还挺快的,最先想到的肯定就是转成字符串,然后一位位比较判断,位数无非也就是两种可能,奇数和偶数。如果说每一位都相等那就返回true。否则返回false。
代码如下:

class Solution {
public:
    bool isPalindrome(int x) {
        string str = std::to_string(x);
        int i,j,res=0;
        if(str.length()%2==0)
        {
            for(i=0,j = str.length()-1;i<str.length()/2,j>=str.length()/2;i++,j--)
            {
                if(str[i]==str[j])
                    res++;
            }
        } else{
            for(i=0,j = str.length()-1;i<str.length()/2,j>str.length()/2;i++,j--)
            {
                if(str[i]==str[j])
                    res++;
            }
        }
        if(res==str.length()/2)
            return true;
        return false;
    }
};

执行用时:12 ms, 在所有 C++ 提交中击败了75.73%的用户
内存消耗:5.8 MB, 在所有 C++ 提交中击败了76.34%的用户

BUT:有一个难度升级。
Follow up: Could you solve it without converting the integer to a string?
不转为字符串直接判断,首先能够想到的是,如果是负数,那肯定返回false。否则的话肯定要先获取到位数。然后一位位比较判断。
其次还要用的一个函数,那就是10的多少次方用pow(10,x)表示。

最终代码如下:

class Solution {
public:
    bool isPalindrome(int x) {
        int len=0,tmp=x,tmp1,res=0,i,num=0;
        int tmp2;
        if(x<0)
            return false;
        while(tmp)
        {
            tmp/=10;
            len++;
        }
        tmp1=len/2;
        if(len%2==0)
        {
            for(i=0;i<tmp1;i++)
            {
                if(num){
                    tmp2 = x/pow(10,len-i-1-num);
                    tmp2%=10;
                } else {
                    tmp2 = x/pow(10,len-i-1);
                }
                if(x%10==tmp2)
                {
                    x/=10;
                    res++;
                    num++;
                } else {
                    return false;
                }
            }
        } else {
            for(i=0;i<tmp1;i++)
            {
                if(num){
                    tmp2 = x/pow(10,len-i-1-num);
                    tmp2%=10;
                } else {
                    tmp2 = x/pow(10,len-i-1);
                }
                if(x%10==tmp2)
                {
                    x/=10;
                    res++;
                    num++;
                } else {
                    return false;
                }
            }
        }
        if(res==len/2)
            return true;
        return false;
    }
};

执行用时:12 ms, 在所有 C++ 提交中击败了75.73%的用户
内存消耗:5.9 MB, 在所有 C++ 提交中击败了27.51%的用户

看题解:
还有一种方法,就是只取出一半数字来进行反转,然后与另一半进行比较。
想到了直接反过来比较。但是碰到了一个问题。
如果结尾是0那么必定return false,因为一个数的开头不可能是0,但还要注意,数字0需要return true。
同时这个直接反过来也会遇到溢出问题。采用了Day2的判断溢出的方法哈哈。
代码如下:

class Solution {
public:
    bool isPalindrome(int x) {
        int tmp=0,num=x;
        if(x<0 || (x%10==0&&x!=0))
            return false;
        while(num>tmp)
        {
            if((tmp<-214748364)||(tmp==-214748364&&x%10<-8)||tmp>214748364||(tmp>214748364&&x%10>7))
                return false;
            tmp = tmp*10 + x%10;
            x/=10;        
        }
        return num==tmp;
    }
};

执行用时:4 ms, 在所有 C++ 提交中击败了98.42%的用户
内存消耗:5.8 MB, 在所有 C++ 提交中击败了42.90%的用户

如果是直接取一半的话就不用考虑溢出的问题了,但需要考虑如果是奇数需要去掉多出来的那一位。

class Solution {
public:
    bool isPalindrome(int x) {
        int tmp=0;
        if(x<0 || (x%10==0&&x!=0))
            return false;
        while(x>tmp)
        {
            tmp = tmp*10 + x%10;
            x/=10;        
        }
        //如果是奇数去掉多出来的中间那一位。
        return x==tmp||x==tmp/10;
    }
};

OK!2021-4-17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值