Thursday, October 5, 2017

238. Product of Array Except Self

https://leetcode.com/problems/product-of-array-except-self/description/
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n,1);
        int forward = 1, backward = 1;
       
        for(int i=0; i<n; i++) {
            res[i] *= forward;
            res[n-1-i] *= backward;
            forward *= nums[i];
            backward *= nums[n-1-i];
        }
        return res;
    }

No comments:

Post a Comment