A 80ms Easy-understanding C++ Solution


  • 0
    Y

    Compare the pair numbers of left side and right side respectively.

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x < 0) return false;
            int m = x, n = x;
            int count = 0;
            int ld, rd;
            while(m != 0) {
                m /= 10;
                count++;
            }
            m = x;
            for(int i = 0; i < count; i++) {
                rd = m % 10;
                m /= 10;
                for(int j = count - i - 1; j > 0; j--) {
                    n /= 10;
                }
                ld = n % 10;
                n = x;
                if(rd != ld) return false;
            }
            return true;
        }
    };

  • 0
    L

    I wanna know if the extra space also means the new variable such as"int m".


  • 0
    D

    I think they are!


  • 0
    Y

    I'm not so sure, but at least a temporary variable is needed to count or store. As the hint says, if you are thinking of converting the integer to string, note the restriction of using extra space, O(n) is allocated for storing the string. But here, I only use 6 variables that may not be considered as extra space comparing to O(n).


Log in to reply
 

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