Share my C++ solution,easy to understand


  • 5
    V
    class Solution {
    public:
        bool isPalindrome(int x) {
            if (x < 0) return false;
            if (x < 10) return true;
            
            int high = 0, low = 0, weight = 1;
            
            while (x / weight > 9)
                weight *= 10;
            
            while (x > 0)
            {
                high = x / weight;
                low = x % 10;
                if (low != high)
                    return false;
                //x -= (high * weight);
                x %= weight;
                x /= 10;
                weight /= 100;
            }
            
            return true;
        }
    };

  • 0
    J
    This post is deleted!

Log in to reply
 

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