-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge9.java
More file actions
33 lines (27 loc) · 839 Bytes
/
Challenge9.java
File metadata and controls
33 lines (27 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*Given an integer x, return true if x is a palindrome, and false otherwise. */
// import java.util.HashMap;
// import java.util.HashSet;
// import java.util.Map;
// import java.util.Set;
class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
// Automatically return false if the given integer is negative
return false;
}
long reversed = 0;
long digit = x;
while (digit != 0) {
int num = (int) (digit % 10);
reversed = reversed * 10 + num;
digit = digit / 10;
}
return reversed == x;
}
}
public class Challenge9 {
public static void main(String[] args) {
Solution obj1 = new Solution();
System.out.println(obj1.isPalindrome(12233221));
}
}