An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
LeetCode's Palindrome Number Solution - Java
Intuition
I began by mentally walking through the given test cases and recording the steps I took to solve the problems. I used this to create some pseudocode that modeled my thinking. Following that, I translated my pseudocode into Java. I was confident that my program worked after some thorough testing in addition to the auto tests.
Approach
To allow for string indexing and length, I first converted the int to a string. Then I looped through the string until I reached the halfway point, comparing the numbers on the left and right sides. If they weren't the same, I exited the program immediately by returning false. At the end of the program, if the numbers on the left and right sides were the same, I returned true, indicating that the number entered by the user is a palindrome.
Complexity
- Time complexity:O(n)
Code
GitHub: https://github.com/Verisimilitude11/LeetCode-Problem-Solutions/blob/main/Palindrome%20Number.java
class Solution { public static boolean isPalindrome(int x) { String num = Integer.toString(x); for (int i = 0; i < num.length() / 2; i++) { char rightChar = num.charAt(num.length() - i - 1); char leftChar = (num.charAt(i)); if (!(leftChar == rightChar)) { return false; } } return true; }}
Original Link: https://dev.to/verisimilitude11/leetcodes-palindrome-number-solution-java-597
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To