Skip to content

Commit c37314f

Browse files
committed
Add one more solution of PalindromeNumber task
1 parent dd4b665 commit c37314f

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

src/main/java/by/andd3dfx/numeric/PalindromeNumber.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,56 @@
44
* <pre>
55
* <a href="https://leetcode.com/problems/palindrome-number/">Task description</a>
66
*
7-
* Given an integer x, return true if x is a palindrome, and false otherwise.
7+
* Given an integer x, return true if x is a , and false otherwise.
8+
*
9+
* Example 1:
10+
* Input: x = 121
11+
* Output: true
12+
* Explanation: 121 reads as 121 from left to right and from right to left.
13+
*
14+
* Example 2:
15+
* Input: x = -121
16+
* Output: false
17+
* Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
18+
*
19+
* Example 3:
20+
* Input: x = 10
21+
* Output: false
22+
* Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
823
* </pre>
924
*/
1025
public class PalindromeNumber {
1126

1227
public static boolean isPalindrome(int x) {
28+
char[] digits = String.valueOf(x).toCharArray();
29+
var n = digits.length;
30+
for (int i = 0; i < n / 2; i++) {
31+
if (digits[i] != digits[n - i - 1]) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
public static boolean isPalindrome2(int x) {
1339
if (x < 0) {
1440
return false;
1541
}
42+
if (x == 0) {
43+
return true;
44+
}
45+
int n = (int) Math.log10(x) + 1;
46+
int[] digits = new int[n];
47+
48+
var curr = 0;
49+
while (x > 0) {
50+
digits[curr] = x % 10;
51+
curr++;
52+
x /= 10;
53+
}
1654

17-
var chars = String.valueOf(x).toCharArray();
18-
var n = chars.length;
1955
for (int i = 0; i < n / 2; i++) {
20-
if (chars[i] != chars[n - i - 1]) {
56+
if (digits[i] != digits[n - i - 1]) {
2157
return false;
2258
}
2359
}
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package by.andd3dfx.numeric;
22

3-
import org.junit.Test;
4-
53
import static by.andd3dfx.numeric.PalindromeNumber.isPalindrome;
6-
import static org.junit.Assert.assertFalse;
7-
import static org.junit.Assert.assertTrue;
4+
import static by.andd3dfx.numeric.PalindromeNumber.isPalindrome2;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
import org.junit.Test;
88

99
public class PalindromeNumberTest {
1010

1111
@Test
1212
public void testIsPalindrome() {
13-
assertTrue(isPalindrome(121));
14-
assertTrue(isPalindrome(1568651));
15-
assertFalse(isPalindrome(-121));
16-
assertFalse(isPalindrome(123));
17-
assertFalse(isPalindrome(10));
18-
assertTrue(isPalindrome(0));
13+
assertThat(isPalindrome(121)).isTrue();
14+
assertThat(isPalindrome(1568651)).isTrue();
15+
assertThat(isPalindrome(-121)).isFalse();
16+
assertThat(isPalindrome(10)).isFalse();
17+
assertThat(isPalindrome(123)).isFalse();
18+
assertThat(isPalindrome(1001)).isTrue();
19+
assertThat(isPalindrome(0)).isTrue();
20+
}
21+
22+
@Test
23+
public void testIsPalindrome2() {
24+
assertThat(isPalindrome2(121)).isTrue();
25+
assertThat(isPalindrome2(1568651)).isTrue();
26+
assertThat(isPalindrome2(-121)).isFalse();
27+
assertThat(isPalindrome2(10)).isFalse();
28+
assertThat(isPalindrome2(123)).isFalse();
29+
assertThat(isPalindrome2(1001)).isTrue();
30+
assertThat(isPalindrome2(0)).isTrue();
1931
}
2032
}

0 commit comments

Comments
 (0)