11.4 Palindrome Number (Easy)
Determine whether an integer is a palindrome. Do this without extra space.
A palindrome integer is an integer x for which reverse(x) = x where reverse(x) is x with its digit reversed. Negative numbers are not palindromic.
Example :
Input : 12121 Output : True
Input : 123 Output : False
- Use the same logic as 11.2 Reverse Integer. i.e. you first reverse the integer, then compare if the reversed integer is the same is the orgional input.
Time O(log N)
public class Solution { public boolean isPalindrome(int input) { int number = input; int rev = 0; int rmd = 0; while (number > 0) { rmd = number % 10; rev = rev * 10 + rmd; number = number / 10; } return (rev == input); }