https://github.com/emilwijayasekara/leetcode-9-palindrome-number
LeetCode Problem 9. Palindrome Number - Within this repository, you'll find my solution to the "Palindrome Number" problem on LeetCode. This challenge involves determining whether a given integer is a palindrome or not. My approach converts the integer to a string, allowing for a simple comparison of characters from both ends.
https://github.com/emilwijayasekara/leetcode-9-palindrome-number
java leetcode-java leetcode-solutions
Last synced: 7 months ago
JSON representation
LeetCode Problem 9. Palindrome Number - Within this repository, you'll find my solution to the "Palindrome Number" problem on LeetCode. This challenge involves determining whether a given integer is a palindrome or not. My approach converts the integer to a string, allowing for a simple comparison of characters from both ends.
- Host: GitHub
- URL: https://github.com/emilwijayasekara/leetcode-9-palindrome-number
- Owner: EmilWijayasekara
- Created: 2023-12-15T05:47:47.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-17T07:32:36.000Z (almost 2 years ago)
- Last Synced: 2025-01-12T06:09:57.057Z (9 months ago)
- Topics: java, leetcode-java, leetcode-solutions
- Language: Java
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LeetCode Practice (Day 2)
This repository contains my solutions to various LeetCode problems. I use this space to practice coding, improve my algorithmic skills, and showcase my solutions.
## About the problem
- *Problem Number* : 9
- *Problem Name* : [Palindrome Number](https://leetcode.com/problems/palindrome-number/description/ "https://leetcode.com/problems/palindrome-number/description/")
- *Problem difficulty* : Easy (55.01%) 🟢
- *Category* : [Math](https://leetcode.com/tag/math "https://leetcode.com/tag/math")
- *Programming language used* - [Java](https://www.java.com/en/)In this problem given an integer x, return true if x is a *palindrome*, and false otherwise.
given examples are,>*Example 1:*
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.>*Example 2:*
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.>*Example 3:*
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.## Approach Explanation
[](https://postimg.cc/ygJDTqtT)In my approach, first, I converted the integer into a char array because then the array can be reversed. And using a for loop, I reversed the char array.
[](https://postimg.cc/PLVLZf3x)
Using the for loop, I changed the index of arrays to reverse the order. In this case, if the array length is 3 then,
i=0 then j=2
i=1 then j=1
i=2 then j=0
[](https://postimg.cc/Z95BCvL2)Next, I tried to convert this char array to a string and then to integer type, where i got this error :
Exception in thread "main" java.lang.NumberFormatException: For input string: "9987654321" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.ja a:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at Main.isPalindrome(Main.java:33)
at Main.main(Main.java:3)
Then I had to convert this into a long type.[](https://postimg.cc/sMcGj455)
Finally, I compared both long and int values and then returned the true or false value.
### If you have suggestions for improvement or would like to contribute to this solution, feel free to create a pull request. 🙌😇