Skip to content

Commit 4b40a8d

Browse files
committed
feat: add solutions to lc problem: No.0009
No.0009.Palindrome Number
1 parent 64843c1 commit 4b40a8d

File tree

8 files changed

+200
-93
lines changed

8 files changed

+200
-93
lines changed

solution/0000-0099/0009.Palindrome Number/README.md

+88-33
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:反转一半数字**
59+
60+
我们先判断特殊情况:
61+
62+
- 如果 $x \lt 0$,那么 $x$ 不是回文数,直接返回 `false`
63+
- 如果 $x \gt 0$ 且 $x$ 的个位数是 $0$,那么 $x$ 不是回文数,直接返回 `false`
64+
- 如果 $x$ 的个位数不是 $0$,那么 $x$ 可能是回文数,继续执行下面的步骤。
65+
66+
我们将 $x$ 的后半部分反转,与前半部分进行比较,如果相等,那么 $x$ 是回文数,否则 $x$ 不是回文数。
67+
68+
举个例子,例如 $x = 1221$,我们可以将数字后半部分从 “21” 反转为 “12”,并将其与前半部分 “12” 进行比较,因为二者相等,我们得知数字 $x$ 是回文。
69+
70+
让我们看看如何将后半部分反转。
71+
72+
对于数字 $1221$,如果执行 $1221 \bmod 10$,我们将得到最后一位数字 $1$,要得到倒数第二位数字,我们可以先通过除以 $10$ 将最后一位数字从 $1221$ 中移除,$1221 / 10 = 122$,再求出上一步结果除以 $10$ 的余数,$122 \bmod 10 = 2$,就可以得到倒数第二位数字。
73+
74+
如果继续这个过程,我们将得到更多位数的反转数字。
75+
76+
通过将最后一位数字不断地累乘到取出数字的变量 $y$ 上,我们可以得到以相反顺序的数字。
77+
78+
在代码实现上,我们可以反复“取出” $x$ 的最后一位数字,并将其“添加”到 $y$ 的后面,循环直到 $y \ge x$,如果此时 $x = y$,或者 $x = y / 10$,那么 $x$ 就是回文数。
79+
80+
时间复杂度 $O(\log_{10}(n))$,其中 $n$ 是 $x$。对于每次迭代,我们会将输入除以 $10$,因此时间复杂度为 $O(\log_{10}(n))$。空间复杂度 $O(1)$。
81+
5882
<!-- tabs:start -->
5983

6084
### **Python3**
@@ -64,13 +88,13 @@
6488
```python
6589
class Solution:
6690
def isPalindrome(self, x: int) -> bool:
67-
if x < 0:
91+
if x < 0 or (x and x % 10 == 0):
6892
return False
69-
y, t = 0, x
70-
while t:
71-
y = y * 10 + t % 10
72-
t //= 10
73-
return x == y
93+
y = 0
94+
while y < x:
95+
y = y * 10 + x % 10
96+
x //= 10
97+
return x in (y, y // 10)
7498
```
7599

76100
### **Java**
@@ -80,17 +104,51 @@ class Solution:
80104
```java
81105
class Solution {
82106
public boolean isPalindrome(int x) {
83-
if (x < 0) return false;
84-
int y = 0, t = x;
85-
while (t != 0) {
86-
y = y * 10 + t % 10;
87-
t /= 10;
107+
if (x < 0 || (x > 0 && x % 10 == 0)) {
108+
return false;
109+
}
110+
int y = 0;
111+
for (; y < x; x /= 10) {
112+
y = y * 10 + x % 10;
88113
}
89-
return x == y;
114+
return x == y || x == y / 10;
90115
}
91116
}
92117
```
93118

119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
bool isPalindrome(int x) {
125+
if (x < 0 || (x && x % 10 == 0)) {
126+
return false;
127+
}
128+
int y = 0;
129+
for (; y < x; x /= 10) {
130+
y = y * 10 + x % 10;
131+
}
132+
return x == y || x == y / 10;
133+
}
134+
};
135+
```
136+
137+
### **Go**
138+
139+
```go
140+
func isPalindrome(x int) bool {
141+
if x < 0 || (x > 0 && x%10 == 0) {
142+
return false
143+
}
144+
y := 0
145+
for ; y < x; x /= 10 {
146+
y = y*10 + x%10
147+
}
148+
return x == y || x == y/10
149+
}
150+
```
151+
94152
### **JavaScript**
95153

96154
```js
@@ -99,32 +157,29 @@ class Solution {
99157
* @return {boolean}
100158
*/
101159
var isPalindrome = function (x) {
102-
let str = x + '';
103-
let left = 0,
104-
right = str.length - 1;
105-
while (left < right) {
106-
if (str[left] != str[right]) return false;
107-
left++;
108-
right--;
160+
if (x < 0 || (x > 0 && x % 10 === 0)) {
161+
return false;
162+
}
163+
let y = 0;
164+
for (; y < x; x = ~~(x / 10)) {
165+
y = y * 10 + (x % 10);
109166
}
110-
return true;
167+
return x === y || x === ~~(y / 10);
111168
};
112169
```
113170

114-
### **Go**
171+
### **TypeScript**
115172

116-
```go
117-
func isPalindrome(x int) bool {
118-
if x < 0 {
119-
return false
120-
}
121-
result := 0
122-
y := x
123-
for y != 0 {
124-
result = result * 10 + y%10
125-
y /= 10
126-
}
127-
return result == x
173+
```ts
174+
function isPalindrome(x: number): boolean {
175+
if (x < 0 || (x > 0 && x % 10 === 0)) {
176+
return false;
177+
}
178+
let y = 0;
179+
for (; y < x; x = ~~(x / 10)) {
180+
y = y * 10 + (x % 10);
181+
}
182+
return x === y || x === ~~(y / 10);
128183
}
129184
```
130185

solution/0000-0099/0009.Palindrome Number/README_EN.md

+64-33
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,65 @@
5050
```python
5151
class Solution:
5252
def isPalindrome(self, x: int) -> bool:
53-
if x < 0:
53+
if x < 0 or (x and x % 10 == 0):
5454
return False
55-
y, t = 0, x
56-
while t:
57-
y = y * 10 + t % 10
58-
t //= 10
59-
return x == y
55+
y = 0
56+
while y < x:
57+
y = y * 10 + x % 10
58+
x //= 10
59+
return x in (y, y // 10)
6060
```
6161

6262
### **Java**
6363

6464
```java
6565
class Solution {
6666
public boolean isPalindrome(int x) {
67-
if (x < 0) return false;
68-
int y = 0, t = x;
69-
while (t != 0) {
70-
y = y * 10 + t % 10;
71-
t /= 10;
67+
if (x < 0 || (x > 0 && x % 10 == 0)) {
68+
return false;
69+
}
70+
int y = 0;
71+
for (; y < x; x /= 10) {
72+
y = y * 10 + x % 10;
7273
}
73-
return x == y;
74+
return x == y || x == y / 10;
7475
}
7576
}
7677
```
7778

79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
bool isPalindrome(int x) {
85+
if (x < 0 || (x && x % 10 == 0)) {
86+
return false;
87+
}
88+
int y = 0;
89+
for (; y < x; x /= 10) {
90+
y = y * 10 + x % 10;
91+
}
92+
return x == y || x == y / 10;
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func isPalindrome(x int) bool {
101+
if x < 0 || (x > 0 && x%10 == 0) {
102+
return false
103+
}
104+
y := 0
105+
for ; y < x; x /= 10 {
106+
y = y*10 + x%10
107+
}
108+
return x == y || x == y/10
109+
}
110+
```
111+
78112
### **JavaScript**
79113

80114
```js
@@ -83,32 +117,29 @@ class Solution {
83117
* @return {boolean}
84118
*/
85119
var isPalindrome = function (x) {
86-
let str = x + '';
87-
let left = 0,
88-
right = str.length - 1;
89-
while (left < right) {
90-
if (str[left] != str[right]) return false;
91-
left++;
92-
right--;
120+
if (x < 0 || (x > 0 && x % 10 === 0)) {
121+
return false;
93122
}
94-
return true;
123+
let y = 0;
124+
for (; y < x; x = ~~(x / 10)) {
125+
y = y * 10 + (x % 10);
126+
}
127+
return x === y || x === ~~(y / 10);
95128
};
96129
```
97130

98-
### **Go**
131+
### **TypeScript**
99132

100-
```go
101-
func isPalindrome(x int) bool {
102-
if x < 0 {
103-
return false
104-
}
105-
result := 0
106-
y := x
107-
for y != 0 {
108-
result = result * 10 + y%10
109-
y /= 10
110-
}
111-
return result == x
133+
```ts
134+
function isPalindrome(x: number): boolean {
135+
if (x < 0 || (x > 0 && x % 10 === 0)) {
136+
return false;
137+
}
138+
let y = 0;
139+
for (; y < x; x = ~~(x / 10)) {
140+
y = y * 10 + (x % 10);
141+
}
142+
return x === y || x === ~~(y / 10);
112143
}
113144
```
114145

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(int x) {
4+
if (x < 0 || (x && x % 10 == 0)) {
5+
return false;
6+
}
7+
int y = 0;
8+
for (; y < x; x /= 10) {
9+
y = y * 10 + x % 10;
10+
}
11+
return x == y || x == y / 10;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
func isPalindrome(x int) bool {
2-
if x < 0 {
2+
if x < 0 || (x > 0 && x%10 == 0) {
33
return false
44
}
5-
result := 0
6-
y := x
7-
for y != 0 {
8-
result = result * 10 + y%10
9-
y /= 10
5+
y := 0
6+
for ; y < x; x /= 10 {
7+
y = y*10 + x%10
108
}
11-
return result == x
9+
return x == y || x == y/10
1210
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public boolean isPalindrome(int x) {
3-
if (x < 0) return false;
4-
int y = 0, t = x;
5-
while (t != 0) {
6-
y = y * 10 + t % 10;
7-
t /= 10;
3+
if (x < 0 || (x > 0 && x % 10 == 0)) {
4+
return false;
85
}
9-
return x == y;
6+
int y = 0;
7+
for (; y < x; x /= 10) {
8+
y = y * 10 + x % 10;
9+
}
10+
return x == y || x == y / 10;
1011
}
1112
}

solution/0000-0099/0009.Palindrome Number/Solution.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
* @return {boolean}
44
*/
55
var isPalindrome = function (x) {
6-
let str = x + '';
7-
let left = 0,
8-
right = str.length - 1;
9-
while (left < right) {
10-
if (str[left] != str[right]) return false;
11-
left++;
12-
right--;
6+
if (x < 0 || (x > 0 && x % 10 === 0)) {
7+
return false;
138
}
14-
return true;
9+
let y = 0;
10+
for (; y < x; x = ~~(x / 10)) {
11+
y = y * 10 + (x % 10);
12+
}
13+
return x === y || x === ~~(y / 10);
1514
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def isPalindrome(self, x: int) -> bool:
3-
if x < 0:
3+
if x < 0 or (x and x % 10 == 0):
44
return False
5-
y, t = 0, x
6-
while t:
7-
y = y * 10 + t % 10
8-
t //= 10
9-
return x == y
5+
y = 0
6+
while y < x:
7+
y = y * 10 + x % 10
8+
x //= 10
9+
return x in (y, y // 10)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function isPalindrome(x: number): boolean {
2+
if (x < 0 || (x > 0 && x % 10 === 0)) {
3+
return false;
4+
}
5+
let y = 0;
6+
for (; y < x; x = ~~(x / 10)) {
7+
y = y * 10 + (x % 10);
8+
}
9+
return x === y || x === ~~(y / 10);
10+
}

0 commit comments

Comments
 (0)