File tree Expand file tree Collapse file tree 2 files changed +23
-10
lines changed Expand file tree Collapse file tree 2 files changed +23
-10
lines changed Original file line number Diff line number Diff line change 1
1
public class PalindromeNumber {
2
2
3
+ // 耗时101ms
3
4
public boolean isPalindrome (int x ) {
4
5
if (x < 0 ) {
5
6
return false ;
@@ -16,4 +17,19 @@ public boolean isPalindrome(int x) {
16
17
17
18
return true ;
18
19
}
20
+
21
+ /**
22
+ * 直接给数倒过来看是否相等
23
+ * 耗时103ms
24
+ */
25
+ public boolean isPalindrome2 (int x ) {
26
+ if (x < 0 ) {
27
+ return false ;
28
+ }
29
+ int n = 0 , m = x ;
30
+ for ( ; x > 0 ; x /= 10 ) {
31
+ n = n * 10 + x % 10 ;
32
+ }
33
+ return n == m ;
34
+ }
19
35
}
Original file line number Diff line number Diff line change 2
2
3
3
public class Main {
4
4
5
- public int [] productExceptSelf (int [] nums ) {
6
- int [] res = new int [nums .length ];
7
- int left = 1 , right = 1 ;
8
- for (int i = 0 ; i < nums .length ; i ++) {
9
- res [i ] = left ;
10
- left *= nums [i ];
5
+ public boolean isPalindrome (int x ) {
6
+ if (x < 0 ) {
7
+ return false ;
11
8
}
12
- for ( int i = nums . length - 1 ; i >= 0 ; i --) {
13
- res [ i ] *= right ;
14
- right *= nums [ i ] ;
9
+ int n = 0 , m = x ;
10
+ for ( ; x > 0 ; x /= 10 ) {
11
+ n = n * 10 + x % 10 ;
15
12
}
16
- return res ;
13
+ return n == m ;
17
14
}
18
15
19
16
public static void main (String [] args ) {
You can’t perform that action at this time.
0 commit comments