|
| 1 | +--- |
| 2 | +difficulty: Easy |
| 3 | +tags: |
| 4 | +- Math |
| 5 | +title: Palindrome Number |
| 6 | +--- |
| 7 | + |
| 8 | +# Palindrome Number |
| 9 | + |
| 10 | +## Problem |
| 11 | + |
| 12 | +### Metadata |
| 13 | + |
| 14 | +- tags: Math |
| 15 | +- difficulty: Easy |
| 16 | +- source(leetcode): <https://leetcode.com/problems/palindrome-number/> |
| 17 | +- source(lintcode): <http://www.lintcode.com/en/problem/palindrome-number/> |
| 18 | + |
| 19 | +### Description |
| 20 | + |
| 21 | +Determine whether an integer is a palindrome. Do this without extra space. |
| 22 | + |
| 23 | +click to show spoilers. |
| 24 | + |
| 25 | +**Some hints:** |
| 26 | + |
| 27 | +Could negative integers be palindromes? (ie, -1) |
| 28 | + |
| 29 | +If you are thinking of converting the integer to string, note the restriction |
| 30 | +of using extra space. |
| 31 | + |
| 32 | +You could also try reversing an integer. However, if you have solved the |
| 33 | +problem "Reverse Integer", you know that the reversed integer might overflow. |
| 34 | +How would you handle such case? |
| 35 | + |
| 36 | +There is a more generic way of solving this problem. |
| 37 | + |
| 38 | +## 题解1 - 循环处理首尾数字 |
| 39 | + |
| 40 | +题意为判断数字是否为回文,要求为不允许使用额外空间,也就是说不能使用类似数字转字符串的方法直接判断。既然不能使用和数字等长的数组空间,那不借助数组来循环判断首尾数字是否相等总是可以的。接下来的问题就转化为怎么获取数字的首尾数字,获取整数的末尾是非常容易的,对10取模即可,那如何获取整数的首部数字呢?用当前整数除以10的幂(幂的大小和整数的宽度一样)即可。确定好初始和循环终止条件即可。 |
| 41 | + |
| 42 | +### Java |
| 43 | + |
| 44 | +```java |
| 45 | +class Solution { |
| 46 | + public boolean isPalindrome(int x) { |
| 47 | + if (x < 0) return false; |
| 48 | + |
| 49 | + int mod = 1000000000; |
| 50 | + while (x / mod == 0 && (mod > 1)) { |
| 51 | + mod /= 10; |
| 52 | + } |
| 53 | + while (mod > 1) { |
| 54 | + if (x / mod != x % 10) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + x = (x % mod) / 10; |
| 58 | + mod /= 100; |
| 59 | + } |
| 60 | + return true; |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### 源码分析 |
| 66 | + |
| 67 | +对于32位整数来说,初始化最大的除数为 1000000000, 循环找出适合当前的最大的除数,随后算出首尾的数字并对其进行比对,循环退出条件为首尾不匹配或者除数为1(比对至最后一位). |
| 68 | + |
| 69 | +### 复杂度分析 |
| 70 | + |
| 71 | +未使用数组,空间复杂度为 $$O(1)$$. 求最大除数时时间复杂度为数字长度的对数 $$logN$$,判断整数是否回文最差情况下为 $$logN$$, 故综合仍为 $$logN$$. |
| 72 | + |
| 73 | +## 题解2 - 逆序比对 |
| 74 | + |
| 75 | +除了解法1中对整数首尾数字进行一一比对之外,还有一种解法则是先得到逆序的数字输出(求模即可),然后比对逆序输出构建的整数和原整数值,若相等则为回文。这里需要注意的则是在不借助多余空间的情况下构建。 |
| 76 | + |
| 77 | +### Java |
| 78 | + |
| 79 | +```java |
| 80 | +class Solution { |
| 81 | + public boolean isPalindrome(int x) { |
| 82 | + if (x < 0) return false; |
| 83 | + |
| 84 | + int prev = 0; |
| 85 | + int y = x; |
| 86 | + while (y > 0) { |
| 87 | + prev = prev * 10 + y % 10; |
| 88 | + y /= 10; |
| 89 | + } |
| 90 | + if (prev == x) { |
| 91 | + return true; |
| 92 | + } else { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### 源码分析 |
| 100 | + |
| 101 | +由于构建过程中依赖上一次获得的整数值,故初始化上一个整数为 0, 不断累积原整数对10取模后末尾的值,同时进位10. |
| 102 | + |
| 103 | +### 复杂度分析 |
| 104 | + |
| 105 | +空间复杂度为 $$O(1)$$, 时间复杂度为 $$logN$$. |
0 commit comments