Reverse int and check / O(1) space / 120 ms / C++


  • 0
    P
    class Solution {
    public:
        bool isPalindrome(int x) {
            long new_num = 0;
            long num = (long)x;
            while(num > 0) {
                new_num = new_num*10 + num%10;
                num /= 10;
            }
            
            return (new_num == (long)x ? true : false);
        }
    };

  • 0
    S

    the return clause can be just "return new_num ==(long)x",no need to use operator ?:


Log in to reply
 

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