Skip to content

Commit 5124ff2

Browse files
committed
fd
1 parent 8c6c516 commit 5124ff2

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

leetcode/solution/src/PalindromeNumber.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
public class PalindromeNumber {
22

3+
// 耗时101ms
34
public boolean isPalindrome(int x) {
45
if (x < 0) {
56
return false;
@@ -16,4 +17,19 @@ public boolean isPalindrome(int x) {
1617

1718
return true;
1819
}
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+
}
1935
}

leetcode/src/Main.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
public class Main {
44

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;
118
}
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;
1512
}
16-
return res;
13+
return n == m;
1714
}
1815

1916
public static void main(String[] args) {

0 commit comments

Comments
 (0)