Skip to content

Commit afa3a47

Browse files
[N-0] refactor 9
1 parent a2f7f35 commit afa3a47

File tree

3 files changed

+89
-22
lines changed

3 files changed

+89
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ Your ideas/fixes/algorithms are more than welcome!
658658
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_12.java)|O(1)|O(1)|Medium|
659659
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_11.java)|O(n)|O(1)|Medium|
660660
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_10.java)|O(m*n)|O(m*n)|Hard|DP
661-
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_9.java)| O(logn)/(n) | O(1) | Easy
661+
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_9.java)| O(n) | O(1) | Easy
662662
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_8.java)| O(n) | O(1) | Medium
663663
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_7.java) | O(1) | O(1) | Easy |
664664
|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_6.java) | O(n) | O(n) | Easy |
Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 9. Palindrome Number
5+
*
46
* Determine whether an integer is a palindrome. Do this without extra space.
5-
* <p>
7+
*
68
* Some hints:
9+
*
710
* Could negative integers be palindromes? (ie, -1)
8-
* <p>
11+
*
912
* If you are thinking of converting the integer to string, note the restriction of using extra space.
10-
* <p>
13+
*
1114
* You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
12-
* <p>
15+
*
1316
* There is a more generic way of solving this problem.
1417
*/
1518
public class _9 {
16-
17-
/**Purely my original solution: just reverse the entire number and compare with itself, return if they two are equal or not.*/
18-
public boolean isPalindrome(int x) {
19-
if (x == 0) {
20-
return true;
21-
}
22-
if (x < 0) {
23-
return false;
24-
}
25-
int rev = 0;
26-
int tmp = x;
27-
while (tmp != 0) {
28-
rev *= 10;
29-
rev += tmp % 10;
30-
tmp /= 10;
19+
20+
public static class Solution1 {
21+
public boolean isPalindrome(int x) {
22+
if (x == 0) {
23+
return true;
24+
}
25+
if (x < 0) {
26+
return false;
27+
}
28+
int rev = 0;
29+
int tmp = x;
30+
while (tmp != 0) {
31+
rev *= 10;
32+
rev += tmp % 10;
33+
tmp /= 10;
34+
}
35+
return rev == x;
3136
}
32-
return rev == x;
3337
}
3438

35-
/**Then I turned to Discuss and found a more efficient way: reversing only half and then compare if they're equal.*/
39+
/**credit: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow
40+
* reversing only half and then compare if they're equal.*/
41+
public static class Solution2 {
42+
public boolean isPalindrome(int x) {
43+
if (x < 0) {
44+
return false;
45+
} else if (x == 0) {
46+
return true;
47+
} else if (x % 10 == 0) {
48+
return false;
49+
}
50+
int reversed = 0;
51+
while (x > reversed) {
52+
int digit = x % 10;
53+
reversed *= 10;
54+
reversed += digit;
55+
x /= 10;
56+
}
57+
return (x == reversed || x == reversed / 10);
58+
}
59+
}
3660

3761
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._9;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _9Test {
10+
private static _9.Solution1 solution1;
11+
private static _9.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _9.Solution1();
16+
solution2 = new _9.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(false, solution1.isPalindrome(2147483647));
22+
assertEquals(false, solution2.isPalindrome(2147483647));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(true, solution1.isPalindrome(0));
28+
assertEquals(true, solution2.isPalindrome(0));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(true, solution1.isPalindrome(1));
34+
assertEquals(true, solution2.isPalindrome(1));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
assertEquals(false, solution1.isPalindrome(10));
40+
assertEquals(false, solution2.isPalindrome(10));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)