Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 503 Bytes

238. Product of Array Except Self.md

File metadata and controls

21 lines (19 loc) · 503 Bytes
class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> ans(n), front(n, 1), back(n, 1);
        for (int i = 1; i < n; ++i) {
            front[i] = front[i - 1] * nums[i - 1];
        }
        for (int i = n - 2; i >= 0; --i) {
            back[i] = back[i + 1] * nums[i + 1];
        }	
        
        for (int i = 0; i < n; ++i) {
            ans[i] = front[i] * back[i];
        }
        return ans;
    }
};