9. Palindrome Number

本文介绍了一种高效判断整数是否为回文的方法,包括使用除法和模运算重建结果进行比较,以及通过数学运算比较首位数字的方法,避免了溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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

click to show spoilers.

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.

链接: http://leetcode.com/problems/palindrome-number/

题解:

可以用除法和模运算rebuild一个结果,然后比较和输入是否相等。这里其实有可能会overflow,就是在 result * 10的时候,但越界的话也会返回一个32位整数,这个整数是 result * 10结果转换为64位里面的low 32位,一般来说与输入不同。所以结果返回false,也能过AC,但是不严谨。

Time Complexity - O(logx), Space Complexity - O(1)。

public class Solution {
    public boolean isPalindrome(int x) {
        if(x == 0)
            return true;
        int result = 0, temp = x;    
        
        while(temp > 0){
            result = 10 * result + temp % 10;
            temp /= 10;
        }
        
        return result == x;
    }
}

 

另外一种方法可以避免overflow。先用对数计算出x有几位,然后通过数学运算比较第一位和最后一位是否相等,接下来去掉最大的一位和最小的一位,再将digitNum - 2,继续进行计算。

Time Complexity - O(logx), Space Complexity - O(1)。

public class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0)
            return false;
        int digitNum= (int)(Math.log(x) / Math.log(10));    
        int left = 0, right = 0;
        
        while(x > 0){
            int powInTens = (int)Math.pow(10, digitNum);
            left = x / powInTens;
            right = x % 10;
            if(left != right)
                return false;
            x -= left * powInTens;
            x /= 10;
            digitNum -= 2 ;
        }
        
        return true;
    }
}

 

二刷:

Java: - Reverse all digits:

public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        int res = 0, tmp = x;
        while (tmp > 0) {
            res = res * 10 + tmp % 10;
            tmp /= 10;
        }
        return res == x;
    }
}

Reverse half digits:

public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0 || (x != 0 && x % 10 == 0)) {
            return false;
        }
        int res = 0;
        while (x > res) {
            res = res * 10 + x % 10;
            x /= 10;
        }
        return (res == x) || (x == res / 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
        res = 0
        while x > res:
            res = res * 10 + x % 10
            x /= 10
        return (x == res) or (x == res / 10)
        
        

 

三刷:

Java:

计算全部digits:  设立一个int n = x, 然后我们计算一下n的按位反转数res,最后比较 n == res

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

 

计算一半digits:

这里我们要先剪掉特殊情况 x % 10 == 0,这时候假如x不为0的话,肯定不是palindromic number。

之后while循环作比较的条件是  x  > res。 我们只需要计算一半的digits,然后比较是否 x == res, 或者 x == res / 10。

当x是偶数长度的话,我们比较 x == res; 当x时奇数长度的时候,比如 x = 1,这时我们比较的是 x == res / 10。合起来用一个或运算就可以了。

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

 

 

 

Reference:

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

https://leetcode.com/discuss/23563/line-accepted-java-code-without-the-need-handling-overflow

https://leetcode.com/discuss/12693/neat-ac-java-code-o-n-time-complexity 

https://leetcode.com/discuss/65915/python-solution-with-other-variable-introduced-besides-input

 

转载于:https://www.cnblogs.com/yrbbest/p/4430410.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值