-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeNumber.java
More file actions
52 lines (46 loc) · 1.32 KB
/
PalindromeNumber.java
File metadata and controls
52 lines (46 loc) · 1.32 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package BasicPrograms;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 21 April 2025
* @link 9. Palindrome Number <a href="https://leetcode.com/problems/palindrome-number/description/"> LeetCode link</a>
* @topics Math
*/
public class PalindromeNumber {
public static void main(String[] args) {
int x = 532435;
System.out.println(isPalindrome(x));
System.out.println(isPalindrome2(x));
System.out.println(isPalindrome3(x));
}
public static boolean isPalindrome(int x) {
if(x<0) {
return false;
}
int originalX = x;
int reverseX = 0;
while(x>0) {
int lastDigit = x%10;
x /= 10;
reverseX = reverseX*10 + lastDigit;
}
return originalX == reverseX;
}
public static boolean isPalindrome2(int x) {
if (x < 0)
return false;
int y = 0; // reverse of x
int temp = x;
while (temp != 0) {
y = y * 10 + temp % 10; // 0 + 0 // 0 + 1
temp = temp / 10; // 1
}
return x == y;
}
public static boolean isPalindrome3(int x) {
if(x<0) {
return false;
}
String s = String.valueOf(x);
return new StringBuilder(s).reverse().toString().equals(s);
}
}