Software engineer

LC-Palindrome Number

LC-Palindrome Number

Palindrome Number

Question

Given an integer x, return true if x is a palindrome, and false otherwise.

Input: x = 121

Output: true

Explanation: 121 reads as 121 from left to right and from right to left.

class Solution {
  public boolean isPalindrome(int x) {
    if(x<0) return false;
    if(x%10==0&& x!=0) return false;
    int res=0;
    while(x>res){
      res=res*10+x%10;
      x/=10;
    }
    return res/10==x||res==x;
  }
}
comments powered by Disqus