Share my reverse and overflow free solution


  • 0
    X

    My solution is based on reverse number. And the overflow is also taken into consideration.

    class Solution {
    public:
        bool isPalindrome(int x) {
            if (x < 0) return false;
    
            int reverse = 0;
            int power = 1;
            while (x/power/10) {
                    power *= 10;
                    reverse = reverse * 10 + x/power%10;
            }
            return x%power == reverse;
        }
    };

Log in to reply
 

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