Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq7 committed Oct 21, 2024
1 parent b5d7a6f commit c8dc932
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/Knowledge/Base.md
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,44 @@ djb2 是一个产生随机分布的的哈希函数
return ans
};
```
## 前缀和
- :yellow_circle: [238. 除自身以外数组的乘积](https://leetcode.cn/problems/product-of-array-except-self/description/)
预先计算 左、右前缀和, l[i] 代表 i 左边所有数字的乘积, r[i] 代表 i 右边所有数字的乘积
遍历修改 nums[i] = l[i] \* r[i], 这里 0 索引和 n-1 索引需要特殊处理, 因为没有 l[0] 和 r[n-1]
```Js
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const n = nums.length
const l = new Array(n).fill(1)
const r = new Array(n).fill(1)

l[1] = nums[0]
r[n - 2] = nums[n - 1]
for (let i = 1; i < n; i++) {
l[i] = nums[i - 1] * l[i - 1]
}
for (let i = n - 2; i >= 0; i--) {
r[i] = nums[i + 1] * r[i + 1]
}

for (let i = 0; i < n; i++) {
if (i === 0) {
nums[i] = r[i]
} else if (i === n - 1) {
nums[i] = l[i]
} else {
nums[i] = l[i] * r[i]
}
}

return nums
};
```

0 comments on commit c8dc932

Please sign in to comment.