Share my C++ solution with constant space complexity,easy to understand


  • 5
    V
    class Solution {
    public:
        vector<int> productExceptSelf(vector<int>& nums) {
            int n = nums.size(), i = 0;
            vector<int> output(n, 1);
            
            int k = nums[n-1];
            for (i = 1; i < n; ++i)
                output[i] = output[i-1] * nums[i-1];
            for (i = n - 2; i >=0; --i)
            {
                output[i] *= k;
                k *= nums[i];
            }
            
            return output;
        }
    };

Log in to reply
 

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