Product of Array Except Self

https://leetcode.com/problems/product-of-array-except-self/

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {

        vector<int> result;
        int n = nums.size();
        if(n == 0)
        {
            return result;
        }

        vector<int> arr(n+1, 1);
        for(int i = n-1; i >= 0; i--)
        {
            arr[i] = arr[i+1] * nums[i];
        }

        int tmp = 1;
        for(int i = 0; i<n; i++)
        {
            result.push_back(tmp*arr[i+1]);
            tmp = tmp * nums[i];
        }

        return result;
    }
};

results matching ""

    No results matching ""