7 lines c++ solution


  • 1
    C
    class Solution {
    public:
        bool isPalindrome(int x) {
            int result = 0;
            int num = x;
            while (num>0){
                int tmp = result;
                result = result*10 + num%10;
                if ((result/10)!=tmp) return false; //will return false if overflow
                num/=10;
            }
            return result==x;
        }
    };

  • 0
    Y

    but How to do with the overflow.....


  • 0
    C

    Hi, Thanks for the comment, the code has been revised. Please let me know if it doesn't solve the problem.


Log in to reply
 

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