Share my C++ solution & easy understand.


  • -2
    J
    class Solution {
    public:
    bool isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
    
        char str[16];
        sprintf(str, "%d", x);
    
        size_t n = strlen(str);
    
        for (int i = 0; i < n; i++) {
            if (str[i] != str[n-i-1]) {
                return false;
            }
        }
    
        return  true;
    }
    };

  • 0
    K

    I guess extra space for storing the string format of the number cannot be used.


Log in to reply
 

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