zoukankan      html  css  js  c++  java
  • 9. Palindrome Number

    题目:

    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

  • 相关阅读:
    校验器
    Mybatis分页中遇到的坑3

    Lock1
    Validation(4)-临时
    在Java中如何判断对象已死?
    垃圾回收算法的种类
    Java内存区域
    Java 中的 volatile关键字含义
    分别写出堆内存溢出与栈内存溢出的程序?
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4430410.html
Copyright © 2011-2022 走看看