Palindrome Number LeetCode Solution

Difficulty Level Medium
Frequently asked in Adobe Amazon Apple Bloomberg Cognizant Infosys Microsoft TCS Uber YahooViews 11300

Problem Statement

Palindrome Number LeetCode Solution says that –

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.

Constraints:

  • -231 <= x <= 231 - 1

Algorithm:

Idea:

  • In order to find the Palindrome Number. First, we will focus on the palindrome if the given number is equal to the reverse of the given number then we can say that the number is a palindrome and return True else return False.

Approach:

  • First, we will make one variable Total, and initially, the value of the variable will be zero then we will use the condition that the given number should be greater than zero then we will find the modulo of that number and add that number into the total by using total = total*10+number and at last, we will update the number by number // 10.
  • At last, we will return the Total.

 

Palindrome Number LeetCode Solution

Code:

Palindrome Number Python LeetCode Solution:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        
        total = 0
        k = x
        while(x > 0):
            b = x%10
            total = total*10+b
            x = x//10
    
        if total == k:
            return True
        else:
            return False

Palindrome Number Java LeetCode Solution:

class Solution {
    public boolean isPalindrome(int x) {
        int total = 0;
        int k = x;
        while(x > 0){
            
            int b = x%10;
            total = total*10 + b;
            x = x/10;
        
        }
        if(total == k)
            return true;
        return false;
    }
}

Complexity Analysis :

Time complexity:

O(N).

Space complexity:

O(n).

Similar Problem: https://tutorialcup.com/interview/string/shortest-palindrome.htm

Translate »