Palindrome Number


  • 0
    A

    We will store reverse of x in y and then compare if both numbers are equal.

    class Solution {
    public:
        bool isPalindrome(int x) {
            
            // If x is negative return 0
            if(x < 0) {
                return false;
            }
            // If x is single digit return 1
            if(x < 10) {
                return true;
            }
            
            int y = 0;
            int x_copy = x;
            
            while(x) {
               
                y *= 10;
                y += x % 10;
                x /= 10;
            }
            
            if(y == x_copy) {
                return 1;
            }
            
            return 0;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.