LeetCode 刷题记录 9. Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true
Example 2:

Input: -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: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:

Coud you solve it without converting the integer to a string?

方法1:
很容易想到反转数字 然后进行比较 但是会有溢出的问题,所以我们使用long long 存储反转后的数字
c++:

class Solution {
public:
    bool isPalindrome(int x) {
        int temp = x;
        if(x < 0) return false;
        //if(x == 0) return true;
        long long reverse = 0;
        while(temp){
            reverse = reverse * 10 + (temp % 10);
            temp /= 10;
        }
        
        bool flag;
        
        flag = (reverse == x ) ? true : false;
        return flag;
        
    }
};

方法2:
不断取第一位和最后一位比较 如果相等 则继续 直到比较完成或者不一致为止
首先要找到最大的除数div 比如1991,div为1000
然后找到最左边一位 int left = x / div;
最右边一位 int right = x % 10; 判断是否相等
如果不相等直接返回 相等继续比较
x = (x % div) / 10;起到插头去位的作用 由于少了两位 div 要除以100

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        int div = 1;
        while(x / div >= 10) div *= 10;
        while(x > 0){
            int left = x / div;
            int right = x % 10;
            if(left != right) return false;
            x = (x % div) / 10;
            div /= 100;
        }
        return true;
        
    }
};

java:

class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0) return false;
        int div = 1;
        while(x / div >= 10) div *= 10;
        while(x > 0){
            int left = x / div;
            int right = x % 10;
            if(left != right) return false;
            x = (x % div) / 10;
            div /= 100;
        }
        return true;
    }
}

Python:
python中是False和True

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0: return False;
        div = 1
        while x / div >= 10: div *= 10;
        while x > 0:
            left = x / div
            right = x % 10
            if left != right: return False
            x = (x % div) / 10
            div /= 100
        
        return True
        

方法3:

首先特例:x的尾数为0且本身不为0 肯定不是回文数予以排除
然后将数字的后半部分反转与前半部分比较
分两种情况x长度是奇数 例如19191 此时reverse为191 x为19 故判断x == reverse / 10
x长度是偶数 例如1991 此时reverse为19 x为19 故判断x == reverse
C++:

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

java:

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

Python:

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0 or (x != 0 and x % 10 == 0): return False
        reverse = 0
        
        while x > reverse:
            
            reverse = reverse * 10 + x % 10
            x /= 10
        
        return (x == reverse) or (x == reverse / 10)
        

方法4:
换一种想法 如果是回文数 他的回文数还是他自己 故不会溢出 溢出即不是回文数
即可调用之前已经写好的函数
c++:

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

java:

class Solution {
    public boolean isPalindrome(int x) {
       if(x < 0 || (x != 0 && x % 10 == 0)) return false;
        
        
        return x == reverse(x);
    }
    public int reverse(int x) {
        int ans = 0;
        
        while(x != 0){
            
            int remainder = x % 10;
            int newAns = ans * 10 + remainder;
            if((newAns - remainder) / 10 != ans) return 0;
            ans = newAns;
            
            x /= 10;
            
        }
        return ans;
    }
}

python:

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0 or (x != 0 and x % 10 == 0): return False
        
        
        return x == self.reverse(x)
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        
        
        
        factor = -1 if x < 0 else 1
        
        if factor == 1:
            reverseX = int(str(x)[::-1])
        else:
            reverseX = int(str(x)[1:][::-1])
            
        
        return 0 if reverseX > (pow(2, 31) - 1) else factor * reverseX
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值