[2017.12.09]9. Palindrome Number 回文数字

来源:互联网 发布:文件同步备份软件 编辑:程序博客网 时间:2024/06/05 20:22

9. Palindrome Number 回文数字

Problem:

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.

Approach #1 Revert half of the number [Accepted]

Intuitio

The first idea that comes to mind is to convert the number into string, and check if the string is a palindrome, but this would require extra non-constant space for creating the string which is not allowed by the problem description.

Second idea would be reverting the number itself, and then compare the number with original number, if they are the same, then the number is a palindrome. However, if the reversed number is larger than \text{int.MAX}int.MAX, we will hit integer overflow problem.

Following the thoughts based on the second idea, to avoid the overflow issue of the reverted number, what if we only revert half of the \text{int}int number? After all, the reverse of the last half of the palindrome should be the same as the first half of the number, if the number is a palindrome.

For example, if the input is 1221, if we can revert the last part of the number “1221” from “21” to “12”, and compare it with the first half of the number “12”, since 12 is the same as 12, we know that the number is a palindrome.

Let’s see how we could translate this idea into an algorithm.

Algorithm

First of all we should take care of some edge cases. All negative numbers are not palindrome, for example: -123 is not a palindrome since the ‘-’ does not equal to ‘3’. So we can return false for all negative numbers.

所有的负数不可能有回文

Now let’s think about how to revert the last half of the number. For number 1221, if we do 1221 % 10, we get the last digit 1, to get the second to the last digit, we need to remove the last digit from 1221, we could do so by dividing it by 10, 1221 / 10 = 122.
Then we can get the last digit again by doing a modulus by 10, 122 % 10 = 2, and if we multiply the last digit by 10 and add the second last digit, 1 * 10 + 2 = 12, it gives us the reverted number we want.
Continuing this process would give us the reverted number with more digits.

Now the question is, how do we know that we’ve reached the half of the number?

Since we divided the number by 10, and multiplied the reversed number by 10, when the original number is less than the reversed number, it means we’ve processed half of the number digits.

Complexity Analysis**重点内容**

Time complexity : O(log​10 n).
We divided the input by 10 for every iteration, so the time complexity is O(log​10​​ n)

Space complexity : O(1).

Code:

class Solution {    public boolean isPalindrome(int x) {        //Specila cases:        //As discussed above,when x<0,x is not a palindrome        //Also if the last digit of the number is , inorder to be palindrome,        //the first digit of the number also needs to be 0.        //Only 0 satisfy e property.只有 0 满足这个属性。        if(x<0 || (x % 10 == 0 && x != 0 )){            return false;        }        int revertedNumber =0;        while(x > revertedNumber ){            revertedNumber = revertedNumber * 10 + x % 10;            x /=10;        }        //whe the length is an odd number,we can get rid of the middle digit by revertedNumber/10        //For example when the input is 12321,at th end of the while loop we get x=12,revertedNumber=123;        //sinve the middle digit doesn't matter in palidrom(it will always equal to itself),we can simple get rid of it         //当场都位技术是,我们可以哦那个过 revertedNumber/10 来消除中间数        //因为中间数在 palidrome  中并不重要(它总是等于本身),所以我们可以简单地将其除掉        return  x== revertedNumber || x==revertedNumber/10;    }}
阅读全文
0 0