From d40ae12dd309ddba2cf233bbe3c5be1cff935f29 Mon Sep 17 00:00:00 2001 From: juan Date: Mon, 5 Sep 2022 14:55:17 +0800 Subject: [PATCH] vault backup: 2022-09-05 14:55:17 --- .../Leetcode Product-of-Array-Except-Self.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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