Here is my AC code, is my solution in line with the subject using the vector<int>. Runtime :888ms


  • 0
    F

    This problem asks us not to do it with extra space, but I use the vector to store all the remainders and my solution is accepted, so i feel confused about my way, is my solution in line with the subject? Or any improvement?

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x < 0)
                return false;
            if(x / 10 == 0)
                return true;
            vector<int> data;
            int len = 0;
            while(x % 10 != 0 || (x % 10 == 0 && x / 10 != 0))
            {
                int y = x % 10;
                x = x / 10;
                data.push_back(y);
                len++;
            }
            for(int i = 0, j = len - 1; i < j; i++, j--)
            {
                if(data[i] != data[j])
                    return false;
            }
            return true;
        }
    };

Log in to reply
 

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