See my concise solution with Constant space and O(n) complexity


  • 8
    B
        class Solution {
    public:
        vector<int> productExceptSelf(vector<int>& nums) {
            vector<int> result;
            int current = 1;
            for (int i = 0;i < nums.size();++ i) {
                result.push_back(current);
                current *= nums[i];
            }
            int n = nums.size();
            current = 1;
            for (int i = n-1;i >= 0;-- i) {
                result[i] = current * result[i];
                current *= nums[i];
            }
            return result;
        }
    };

Log in to reply
 

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