My 56ms C++ Solution, time complexity O(n), space complexity O(1)


  • 0
    I
    vector<int> productExceptSelf(vector<int>& nums) {
            vector<int> outp(nums);
            int n = nums.size();
            int x = 1;
            for (int i = 0; i< n-1 ; i++) {
                x *=nums[i];
                outp[i] = x;
            }
            outp[n-1] = x;
            for (int i = n-2; i > 0 ; i--) {
                outp[i] = outp[i-1] * nums[i+1];
                nums[i] = nums[i+1] * nums[i];
            }
            outp[0] = nums[1];
            return outp;
        }

Log in to reply
 

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