Leetcode 9 Palindrome number Solution in Java | Hindi Coding Community

0

 



Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

 

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.

 

Constraints:

  • -231 <= x <= 231 - 1

 

Follow up: Could you solve it without converting the integer to a string?


Java Code :




class Solution {
//finding reverse of the number using recursion
int rev(int n,int s){
if(n==0) return s;
int sum=s*10+n%10;
return rev(n/10,sum);
}
public boolean isPalindrome(int x) {
if(x<0) return false;
if(rev(x,0)==x) return true;
//if the reverse of the number is
//equal to the number returm true
else return false;
}
}


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !