File tree Expand file tree Collapse file tree 2 files changed +1
-26
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +1
-26
lines changed Original file line number Diff line number Diff line change 1717 */
1818public class _9 {
1919
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 ;
36- }
37- }
38-
3920 /**credit: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow
4021 * reversing only half and then compare if they're equal.*/
41- public static class Solution2 {
22+ public static class Solution1 {
4223 public boolean isPalindrome (int x ) {
4324 if (x < 0 ) {
4425 return false ;
Original file line number Diff line number Diff line change 88
99public class _9Test {
1010 private static _9 .Solution1 solution1 ;
11- private static _9 .Solution2 solution2 ;
1211
1312 @ BeforeClass
1413 public static void setup () {
1514 solution1 = new _9 .Solution1 ();
16- solution2 = new _9 .Solution2 ();
1715 }
1816
1917 @ Test
2018 public void test1 () {
2119 assertEquals (false , solution1 .isPalindrome (2147483647 ));
22- assertEquals (false , solution2 .isPalindrome (2147483647 ));
2320 }
2421
2522 @ Test
2623 public void test2 () {
2724 assertEquals (true , solution1 .isPalindrome (0 ));
28- assertEquals (true , solution2 .isPalindrome (0 ));
2925 }
3026
3127 @ Test
3228 public void test3 () {
3329 assertEquals (true , solution1 .isPalindrome (1 ));
34- assertEquals (true , solution2 .isPalindrome (1 ));
3530 }
3631
3732 @ Test
3833 public void test4 () {
3934 assertEquals (false , solution1 .isPalindrome (10 ));
40- assertEquals (false , solution2 .isPalindrome (10 ));
4135 }
4236
4337}
You can’t perform that action at this time.
0 commit comments