Palindrome Number Leetcode Solution

In this post, we are going to solve the Palindrome Number Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

Palindrome Number Leetcode Solution
Palindrome Number Leetcode Solution

Problem

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

Now, lets see the leetcode solution of Palindrome Number Leetcode Solution.

Palindrome Number Leetcode Solution in Python

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = str(x)
        if x == x[::-1]:
            return True
        else:
            return False

Palindrome Number Leetcode Solution in CPP

class Solution {
public:
    bool isPalindrome(int x) {
        int org = x;
        int rev = 0;
        
        while(org > 0){
            int unit = org%10;
            rev = 10*rev+unit;
            org/=10;
        }
        
        return rev == x;
    }
};

Palindrome Number Leetcode Solution in Java

class Solution {
    public boolean isPalindrome(int x) {
        int org = x;
        int rev = 0;
        
        while(org > 0){
            int unit = org%10;
            rev = 10*rev+unit;
            org/=10;
        }
        
        return rev == x;
    }
}

Note: This problem Palindrome Number is generated by Leetcode but the solution is provided by Chase2learn This tutorial is only for Educational and Learning purposes.

NEXT: Regular Expression Matching Leetcode Solution

Sharing Is Caring

Leave a Comment