Skip to content

Commit

Permalink
chore: 删除旧主题配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
yk committed Feb 19, 2024
1 parent a5655c4 commit 7ef3567
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 105 deletions.
33 changes: 29 additions & 4 deletions content/posts/algorithm/data structure/数组-前缀和数组.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ preSum[i] = preSum[i-1] + nums[i]
preSum[r] - preSum[l-1] // 需要判断 l 为 0 的情况,直接返回 preSum[r],这种的劣势是需要判断边界条件
```

### 构造:[303.区域和检索-数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/)
### 构造:[lc.303 区域和检索-数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/)

```JavaScript
/**
Expand Down Expand Up @@ -86,7 +86,7 @@ NumArray.prototype.sumRange = function (left, right) {
*/
```

### 构造:[304. 二维区域和检索-矩阵不可变](https://leetcode.cn/problems/range-sum-query-2d-immutable/)
### 构造:[lc.304 二维区域和检索-矩阵不可变](https://leetcode.cn/problems/range-sum-query-2d-immutable/)

```js
/**
Expand Down Expand Up @@ -128,7 +128,7 @@ NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
*/
```

### 应用:[560.和为 k 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/)
### 应用:[lc.560 和为 k 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/)

```JavaScript
/**
Expand All @@ -152,7 +152,32 @@ var subarraySum = function(nums, k) {
};
```

### 进阶: [按权重随机选择](https://leetcode.cn/problems/random-pick-with-weight/)
这样做能得到正确结果,但是并不能 AC,时间复杂度 O(n^2),不知道谁搞了个恶心的测试用例。。。会超时~

看了下题解,可以使用哈希表进行优化

```js
var subarraySum = function (nums, k) {
const mp = new Map()
mp.set(0, 1)
let count = 0,
pre = 0
for (const x of nums) {
pre += x
if (mp.has(pre - k)) {
count += mp.get(pre - k)
}
if (mp.has(pre)) {
mp.set(pre, mp.get(pre) + 1)
} else {
mp.set(pre, 1)
}
}
return count
}
```

### 进阶:[lc.528 按权重随机选择](https://leetcode.cn/problems/random-pick-with-weight/)

```JavaScript
/**
Expand Down
101 changes: 0 additions & 101 deletions tania.yaml

This file was deleted.

0 comments on commit 7ef3567

Please sign in to comment.