Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4217b25

Browse files
Merge pull request SharingSource#534 from SharingSource/ac_oier
✨feat: add 560
2 parents 5268b82 + ed9081f commit 4217b25

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

‎Index/前缀和.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [523. 连续的子数组和](https://leetcode-cn.com/problems/continuous-subarray-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/continuous-subarray-sum/solution/gong-shui-san-xie-tuo-zhan-wei-qiu-fang-1juse/) | 中等 | 🤩🤩🤩🤩 |
1212
| [525. 连续数组](https://leetcode-cn.com/problems/contiguous-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contiguous-array/solution/gong-shui-san-xie-qian-zhui-he-ha-xi-bia-q400/) | 中等 | 🤩🤩🤩🤩 |
1313
| [528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-with-weight/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-8bx50/) | 中等 | 🤩🤩🤩🤩 |
14+
| [560. 和为 K 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/) | [LeetCode 题解链接](https://leetcode.cn/problems/subarray-sum-equals-k/solution/by-ac_oier-pttu/) | 中等 | 🤩🤩🤩🤩🤩 |
1415
| [629. K个逆序对数组](https://leetcode-cn.com/problems/k-inverse-pairs-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/k-inverse-pairs-array/solution/gong-shui-san-xie-yi-dao-xu-lie-dp-zhuan-tm01/) | 中等 | 🤩🤩🤩🤩🤩 |
1516
| [661. 图片平滑器](https://leetcode-cn.com/problems/image-smoother/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/image-smoother/solution/by-ac_oier-nn3v/) | 简单 | 🤩🤩🤩🤩 |
1617
| [689. 三个无重叠子数组的最大和](https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/solution/gong-shui-san-xie-jie-he-qian-zhui-he-de-ancx/) | 困难 | 🤩🤩🤩 |

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
| [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/keyboard-row/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-zx6b/) | 简单 | 🤩🤩🤩🤩 |
2828
| [519. 随机翻转矩阵](https://leetcode-cn.com/problems/random-flip-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-flip-matrix/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-e6gi/) | 中等 | 🤩🤩🤩🤩 |
2929
| [554. 砖墙](https://leetcode-cn.com/problems/brick-wall/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/brick-wall/solution/gong-shui-san-xie-zheng-nan-ze-fan-shi-y-gsri/) | 中等 | 🤩🤩🤩 |
30+
| [560. 和为 K 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/) | [LeetCode 题解链接](https://leetcode.cn/problems/subarray-sum-equals-k/solution/by-ac_oier-pttu/) | 中等 | 🤩🤩🤩🤩🤩 |
3031
| [594. 最长和谐子序列](https://leetcode-cn.com/problems/longest-harmonious-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-harmonious-subsequence/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-quuh/) | 简单 | 🤩🤩🤩🤩 |
3132
| [599. 两个列表的最小索引总和](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/solution/by-ac_oier-oh5b/) | 简单 | 🤩🤩🤩🤩 |
3233
| [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 |
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[560. 和为 K 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/solution/by-ac_oier-pttu/)** ,难度为 **中等**
4+
5+
Tag : 「前缀和」、「哈希表」
6+
7+
8+
9+
给你一个整数数组 `nums` 和一个整数 `k` ,请你统计并返回该数组中和为 `k` 的子数组的个数 。
10+
11+
示例 1:
12+
```
13+
输入:nums = [1,1,1], k = 2
14+
15+
输出:2
16+
```
17+
示例 2:
18+
```
19+
输入:nums = [1,2,3], k = 3
20+
21+
输出:2
22+
```
23+
24+
提示:
25+
* 1ドル <= nums.length <= 2 * 10^4$
26+
* $-1000 <= nums[i] <= 1000$
27+
* $-10^7 <= k <= 10^7$
28+
29+
---
30+
31+
### 前缀和 + 哈希表
32+
33+
这是一道经典的前缀和运用题。
34+
35+
统计以每一个 $nums[i]$ 为结尾,和为 $k$ 的子数组数量即是答案。
36+
37+
我们可以预处理前缀和数组 `sum`(前缀和数组下标默认从 1ドル$ 开始),对于求解以某一个 $nums[i]$ 为结尾的,和为 $k$ 的子数组数量,本质上是求解在 $[0, i]$ 中,`sum` 数组中有多少个值为 $sum[i + 1] - k$ 的数,这可以在遍历过程中使用「哈希表」进行同步记录。
38+
39+
代码:
40+
```Java
41+
class Solution {
42+
public int subarraySum(int[] nums, int k) {
43+
int n = nums.length, ans = 0;
44+
int[] sum = new int[n + 10];
45+
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nums[i - 1];
46+
Map<Integer, Integer> map = new HashMap<>();
47+
map.put(0, 1);
48+
for (int i = 1; i <= n; i++) {
49+
int t = sum[i], d = t - k;
50+
ans += map.getOrDefault(d, 0);
51+
map.put(t, map.getOrDefault(t, 0) + 1);
52+
}
53+
return ans;
54+
}
55+
}
56+
```
57+
* 时间复杂度:预处理前缀和的复杂度为 $O(n),ドル统计答案的复杂度为 $O(n)$。整体复杂度为 $O(n)$
58+
* 空间复杂度:$O(n)$
59+
60+
---
61+
62+
### 最后
63+
64+
这是我们「刷穿 LeetCode」系列文章的第 `No.560` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
65+
66+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
67+
68+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
69+
70+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
71+

‎LeetCode/剑指 Offer II/剑指 Offer II 114. 外星文字典(困难).md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Tag : 「图论」、「拓扑排序」、「建图」、「图论 BFS」
4949

5050
为了方便,我们称 `words``ws`,同时将两个字符串 `a``b` 之间的字典序关系简称为「关系」。
5151

52-
由于数组长度和每个 $ws[i]$ 的最大长度均为 100ドル,ドル我们可以实现复杂度为 $O(n^3)$ 复杂度的算法
52+
由于数组长度和每个 $ws[i]$ 的最大长度均为 100ドル,ドル我们可以实现复杂度为 $O(n^3)$ 的算法
5353

5454
首先容易想到,我们从前往后处理每个 $ws[i],ドル利用 `ws` 数组本身已按字典序排序,然后通过 $ws[i]$ 与 $ws[j]$ 的关系(其中 $j$ 的范围为 $[0, i - 1]$),来构建字符之间的关系。
5555

@@ -67,7 +67,7 @@ class Solution {
6767
int N = 26, M = N * N, idx = 0, cnt = 0;
6868
int[] he = new int[N], e = new int[M], ne = new int[M];
6969
int[] in = new int[N], out = new int[N];
70-
boolean[] vis = new boolean[26];
70+
boolean[] vis = new boolean[N];
7171
void add(int a, int b) {
7272
e[idx] = b;
7373
ne[idx] = he[a];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /