diff --git a/OJ notes/pages/Leetcode Product-of-Array-Except-Self.md b/OJ notes/pages/Leetcode Product-of-Array-Except-Self.md index 99e8823..b8badf6 100644 --- a/OJ notes/pages/Leetcode Product-of-Array-Except-Self.md +++ b/OJ notes/pages/Leetcode Product-of-Array-Except-Self.md @@ -96,6 +96,30 @@ loop. Third version: +```cpp +class Solution { +public: + vector productExceptSelf(vector &nums) { + // Optimized one-pass prefix sum & suffix sum + int size = nums.size(); + + vector ans(size, 1); + + int prefix = 1, suffix = 1; + + for (int i = 0; i < size; i++) { + ans[i] *= prefix; + prefix *= nums[i]; + + ans[size - i - 1] *= suffix; + suffix *= nums[size - i - 1]; + } + + return ans; + } +}; +``` + Second version: ```cpp