Header Ad

Leetcode Palindrome Number problem solution

In this Leetcode Palindrome Number problem solution, we have an integer x return true if x is a palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Leetcode Palindrome Number problem solution


Problem solution in Python.

class Solution:
    def isPalindrome(self, x: int) -> bool:
        """ """
        temp = x
        reverse = 0
        while x > 0:
            reverse *= 10
            reverse += x % 10
            x //= 10
        return temp == reverse



Problem solution in Java.

class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0)
            return false;
        else if(String.valueOf(x).length() == 1)
            return true;
        
        String str = String.valueOf(x);
        
        int i = 0;
        int j = str.length() -1;
        while(j > i){
            if(str.charAt(i) != str.charAt(j))
                return false;
            i++;
            j--;
        }
        return true;
    }
}


Problem solution in C++.

class Solution {
public:
    bool isPalindrome(int x) {
            string s = to_string(x);
            int start = 0;
            int end = s.length() - 1;

            while(start < end)
            {
                if(s[start++] != s[end--])
                    return false;
            }

            return true;
        }
};


Problem solution in C.

bool isPalindrome(int x) {
    if (x < 0) return false;
    long y=0;
    long xx=x;
    while (xx>0) {
        y = y*10 + xx%10;
        xx/=10;
    }
    return (y == x);
}


Post a Comment

0 Comments