Skip to content

Commit 48fe3c2

Browse files
authored
Enhance readme with Palindrome Number solution details
Added a detailed explanation and implementation for the Palindrome Number problem in Java, including constraints and follow-up considerations.
1 parent 60c174a commit 48fe3c2

File tree

1 file changed

+60
-1
lines changed
  • src/main/java/g0001_0100/s0009_palindrome_number

1 file changed

+60
-1
lines changed

src/main/java/g0001_0100/s0009_palindrome_number/readme.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,63 @@ Given an integer `x`, return `true` _if_ `x` _is a_ _**palindrome**__, and_ `fal
3232

3333
* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code>
3434

35-
**Follow up:** Could you solve it without converting the integer to a string?
35+
**Follow up:** Could you solve it without converting the integer to a string?
36+
37+
To solve the Palindrome Number problem in Java using a `Solution` class, we'll follow these steps:
38+
39+
1. Define a `Solution` class with a method named `isPalindrome`.
40+
2. Handle special cases where `x` is negative or divisible by 10 but not equal to zero, as they cannot be palindromes.
41+
3. Initialize variables to keep track of the reversed number (`reversed`) and the original number (`original`).
42+
4. Iterate through the digits of the original number `x`:
43+
- Extract the least significant digit using the modulo operator and append it to the reversed number.
44+
- Update `original` by removing the least significant digit using integer division.
45+
5. Check if the reversed number is equal to the original number. If so, return `true`; otherwise, return `false`.
46+
47+
Here's the implementation:
48+
49+
```java
50+
public class Solution {
51+
52+
public boolean isPalindrome(int x) {
53+
// Special cases:
54+
// As discussed, negative numbers are not palindromes.
55+
// Also, if the last digit of the number is 0, it can't be a palindrome unless the number is 0 itself.
56+
if (x < 0 || (x % 10 == 0 && x != 0)) {
57+
return false;
58+
}
59+
60+
int reversed = 0;
61+
int original = x;
62+
63+
while (original > reversed) {
64+
int digit = original % 10;
65+
reversed = reversed * 10 + digit;
66+
original /= 10;
67+
}
68+
69+
// When the length is an odd number, we can get rid of the middle digit by reversed / 10
70+
// For example when the input is 12321, at the end of the while loop we get x = 12, reversed = 123,
71+
// since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it.
72+
return original == reversed || original == reversed / 10;
73+
}
74+
75+
public static void main(String[] args) {
76+
Solution solution = new Solution();
77+
78+
// Test cases
79+
int x1 = 121;
80+
System.out.println("Example 1 Output: " + solution.isPalindrome(x1));
81+
82+
int x2 = -121;
83+
System.out.println("Example 2 Output: " + solution.isPalindrome(x2));
84+
85+
int x3 = 10;
86+
System.out.println("Example 3 Output: " + solution.isPalindrome(x3));
87+
88+
int x4 = -101;
89+
System.out.println("Example 4 Output: " + solution.isPalindrome(x4));
90+
}
91+
}
92+
```
93+
94+
This implementation provides a solution to the Palindrome Number problem in Java.

0 commit comments

Comments
 (0)