vault backup: 2022-09-05 14:55:17
This commit is contained in:
parent
294c59220e
commit
d40ae12dd3
|
@ -96,6 +96,30 @@ loop.
|
|||
|
||||
Third version:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> productExceptSelf(vector<int> &nums) {
|
||||
// Optimized one-pass prefix sum & suffix sum
|
||||
int size = nums.size();
|
||||
|
||||
vector<int> 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
|
||||
|
|
Loading…
Reference in a new issue