Skip to content

Commit 1fd45c1

Browse files
committed
LC#9 checks if given number is Palindrome or not
1 parent 97930f2 commit 1fd45c1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package easy;
2+
3+
public class PalindromeNumber9 {
4+
5+
public static boolean isPalindrome(int x) {
6+
if (x < 0)
7+
return false;
8+
9+
String num = String.valueOf(x);
10+
11+
int left = 0, right = num.length() - 1;
12+
13+
while (left < right) {
14+
if (num.charAt(left) != num.charAt(right)) {
15+
return false;
16+
}
17+
left++;
18+
right--;
19+
}
20+
return true;
21+
}
22+
23+
public static void main(String[] args) {
24+
System.out.println(isPalindrome(121));
25+
System.out.println(isPalindrome(-121));
26+
System.out.println(isPalindrome(12321));
27+
System.out.println(isPalindrome(10));
28+
29+
}
30+
}

0 commit comments

Comments
Β (0)