File tree 4 files changed +53
-21
lines changed
solution/0000-0099/0009.Palindrome Number
4 files changed +53
-21
lines changed Original file line number Diff line number Diff line change 42
42
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
43
44
44
``` python
45
-
45
+ class Solution :
46
+ def isPalindrome (self , x : int ) -> bool :
47
+ if x < 0 :
48
+ return False
49
+ y, t = 0 , x
50
+ while t:
51
+ y = y * 10 + t % 10
52
+ t //= 10
53
+ return x == y
46
54
```
47
55
48
56
### ** Java**
49
57
50
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
59
52
60
``` java
53
-
61
+ class Solution {
62
+ public boolean isPalindrome (int x ) {
63
+ if (x < 0 ) return false ;
64
+ int y = 0 , t = x;
65
+ while (t != 0 ) {
66
+ y = y * 10 + t % 10 ;
67
+ t /= 10 ;
68
+ }
69
+ return x == y;
70
+ }
71
+ }
54
72
```
55
73
56
74
### ** ...**
Original file line number Diff line number Diff line change 51
51
### ** Python3**
52
52
53
53
``` python
54
-
54
+ class Solution :
55
+ def isPalindrome (self , x : int ) -> bool :
56
+ if x < 0 :
57
+ return False
58
+ y, t = 0 , x
59
+ while t:
60
+ y = y * 10 + t % 10
61
+ t //= 10
62
+ return x == y
55
63
```
56
64
57
65
### ** Java**
58
66
59
67
``` java
60
-
68
+ class Solution {
69
+ public boolean isPalindrome (int x ) {
70
+ if (x < 0 ) return false ;
71
+ int y = 0 , t = x;
72
+ while (t != 0 ) {
73
+ y = y * 10 + t % 10 ;
74
+ t /= 10 ;
75
+ }
76
+ return x == y;
77
+ }
78
+ }
61
79
```
62
80
63
81
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean isPalindrome (int x ) {
3
- if (x < 0 ) {
4
- return false ;
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 ;
5
8
}
6
- int res = 0 ;
7
- int y = x ;
8
- while (y != 0 ) {
9
- res = res * 10 + y % 10 ;
10
- y /= 10 ;
11
- }
12
- return res == x ;
9
+ return x == y ;
13
10
}
14
11
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
- def isPalindrome (self , num ):
3
- """
4
- :type num: int
5
- :rtype: bool
6
- """
7
- if num < 0 :
2
+ def isPalindrome (self , x : int ) -> bool :
3
+ if x < 0 :
8
4
return False
9
-
10
- return str (num ) == str (num )[::- 1 ]
5
+ y , t = 0 , x
6
+ while t :
7
+ y = y * 10 + t % 10
8
+ t //= 10
9
+ return x == y
You can’t perform that action at this time.
0 commit comments