Skip to content

Commit 06616ea

Browse files
authored
feat: update lc problems (#3115)
1 parent 51147bf commit 06616ea

File tree

15 files changed

+206
-180
lines changed

15 files changed

+206
-180
lines changed

solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ tags:
3030
<p><strong>示例&nbsp;1:</strong></p>
3131

3232
<pre>
33-
<strong>输入:</strong>nums = [2<code>,5,6,0,0,1,2]</code>, target = 0
33+
<strong>输入:</strong>nums = <code>[2,5,6,0,0,1,2]</code>, target = 0
3434
<strong>输出:</strong>true
3535
</pre>
3636

3737
<p><strong>示例&nbsp;2:</strong></p>
3838

3939
<pre>
40-
<strong>输入:</strong>nums = [2<code>,5,6,0,0,1,2]</code>, target = 3
40+
<strong>输入:</strong>nums = <code>[2,5,6,0,0,1,2]</code>, target = 3
4141
<strong>输出:</strong>false</pre>
4242

4343
<p>&nbsp;</p>

solution/0500-0599/0523.Continuous Subarray Sum/README.md

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,17 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### 方法一
80+
### 方法一:前缀和 + 哈希表
81+
82+
根据题目描述,如果存在两个前缀和模 $k$ 的余数相同的位置 $i$ 和 $j$(不妨设 $j < i$),那么 $\text{nums}[j+1..i]$ 这个子数组的和是 $k$ 的倍数。
83+
84+
因此,我们可以使用哈希表存储每个前缀和模 $k$ 的余数第一次出现的位置。初始时,我们在哈希表中存入一对键值对 $(0, -1)$,表示前缀和为 $0$ 的余数 $0$ 出现在位置 $-1$。
85+
86+
遍历数组时,我们计算当前前缀和的模 $k$ 的余数,如果当前前缀和的模 $k$ 的余数没有在哈希表中出现过,我们就将当前前缀和的模 $k$ 的余数和对应的位置存入哈希表中。否则,如果当前前缀和的模 $k$ 的余数在哈希表中已经出现过,位置为 $j$,那么我们就找到了一个满足条件的子数组 $\text{nums}[j+1..i]$,因此返回 $\text{True}$。
87+
88+
遍历结束后,如果没有找到满足条件的子数组,我们返回 $\text{False}$。
89+
90+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\text{nums}$ 的长度。
8191

8292
<!-- tabs:start -->
8393

@@ -86,15 +96,14 @@ tags:
8696
```python
8797
class Solution:
8898
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
99+
d = {0: -1}
89100
s = 0
90-
mp = {0: -1}
91-
for i, v in enumerate(nums):
92-
s += v
93-
r = s % k
94-
if r in mp and i - mp[r] >= 2:
101+
for i, x in enumerate(nums):
102+
s = (s + x) % k
103+
if s not in d:
104+
d[s] = i
105+
elif i - d[s] > 1:
95106
return True
96-
if r not in mp:
97-
mp[r] = i
98107
return False
99108
```
100109

@@ -103,18 +112,16 @@ class Solution:
103112
```java
104113
class Solution {
105114
public boolean checkSubarraySum(int[] nums, int k) {
106-
Map<Integer, Integer> mp = new HashMap<>();
107-
mp.put(0, -1);
115+
Map<Integer, Integer> d = new HashMap<>();
116+
d.put(0, -1);
108117
int s = 0;
109118
for (int i = 0; i < nums.length; ++i) {
110-
s += nums[i];
111-
int r = s % k;
112-
if (mp.containsKey(r) && i - mp.get(r) >= 2) {
119+
s = (s + nums[i]) % k;
120+
if (!d.containsKey(s)) {
121+
d.put(s, i);
122+
} else if (i - d.get(s) > 1) {
113123
return true;
114124
}
115-
if (!mp.containsKey(r)) {
116-
mp.put(r, i);
117-
}
118125
}
119126
return false;
120127
}
@@ -127,14 +134,15 @@ class Solution {
127134
class Solution {
128135
public:
129136
bool checkSubarraySum(vector<int>& nums, int k) {
130-
unordered_map<int, int> mp;
131-
mp[0] = -1;
137+
unordered_map<int, int> d{{0, -1}};
132138
int s = 0;
133139
for (int i = 0; i < nums.size(); ++i) {
134-
s += nums[i];
135-
int r = s % k;
136-
if (mp.count(r) && i - mp[r] >= 2) return true;
137-
if (!mp.count(r)) mp[r] = i;
140+
s = (s + nums[i]) % k;
141+
if (!d.contains(s)) {
142+
d[s] = i;
143+
} else if (i - d[s] > 1) {
144+
return true;
145+
}
138146
}
139147
return false;
140148
}
@@ -145,17 +153,15 @@ public:
145153
146154
```go
147155
func checkSubarraySum(nums []int, k int) bool {
148-
mp := map[int]int{0: -1}
156+
d := map[int]int{0: -1}
149157
s := 0
150-
for i, v := range nums {
151-
s += v
152-
r := s % k
153-
if j, ok := mp[r]; ok && i-j >= 2 {
158+
for i, x := range nums {
159+
s = (s + x) % k
160+
if _, ok := d[s]; !ok {
161+
d[s] = i
162+
} else if i-d[s] > 1 {
154163
return true
155164
}
156-
if _, ok := mp[r]; !ok {
157-
mp[r] = i
158-
}
159165
}
160166
return false
161167
}
@@ -165,19 +171,16 @@ func checkSubarraySum(nums []int, k int) bool {
165171

166172
```ts
167173
function checkSubarraySum(nums: number[], k: number): boolean {
168-
const n = nums.length;
169-
let prefixSum = 0;
170-
const map = new Map([[0, 0]]);
171-
172-
for (let i = 0; i < n; i++) {
173-
prefixSum += nums[i];
174-
const remainder = prefixSum % k;
175-
176-
if (!map.has(remainder)) {
177-
map.set(remainder, i + 1);
178-
} else if (i - map.get(remainder)! > 0) return true;
174+
const d: Record<number, number> = { 0: -1 };
175+
let s = 0;
176+
for (let i = 0; i < nums.length; ++i) {
177+
s = (s + nums[i]) % k;
178+
if (!d.hasOwnProperty(s)) {
179+
d[s] = i;
180+
} else if (i - d[s] > 1) {
181+
return true;
182+
}
179183
}
180-
181184
return false;
182185
}
183186
```

solution/0500-0599/0523.Continuous Subarray Sum/README_EN.md

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ tags:
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Prefix Sum + Hash Table
80+
81+
According to the problem description, if there exist two positions $i$ and $j$ ($j < i$) where the remainders of the prefix sums modulo $k$ are the same, then the sum of the subarray $\text{nums}[j+1..i]$ is a multiple of $k$.
82+
83+
Therefore, we can use a hash table to store the first occurrence of each remainder of the prefix sum modulo $k$. Initially, we store a key-value pair $(0, -1)$ in the hash table, indicating that the remainder $0$ of the prefix sum $0$ appears at position $-1$.
84+
85+
As we iterate through the array, we calculate the current prefix sum's remainder modulo $k$. If the current prefix sum's remainder modulo $k$ has not appeared in the hash table, we store the current prefix sum's remainder modulo $k$ and its corresponding position in the hash table. Otherwise, if the current prefix sum's remainder modulo $k$ has already appeared in the hash table at position $j$, then we have found a subarray $\text{nums}[j+1..i]$ that meets the conditions, and thus return $\text{True}$.
86+
87+
After completing the iteration, if no subarray meeting the conditions is found, we return $\text{False}$.
88+
89+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.
8090

8191
<!-- tabs:start -->
8292

@@ -85,15 +95,14 @@ tags:
8595
```python
8696
class Solution:
8797
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
98+
d = {0: -1}
8899
s = 0
89-
mp = {0: -1}
90-
for i, v in enumerate(nums):
91-
s += v
92-
r = s % k
93-
if r in mp and i - mp[r] >= 2:
100+
for i, x in enumerate(nums):
101+
s = (s + x) % k
102+
if s not in d:
103+
d[s] = i
104+
elif i - d[s] > 1:
94105
return True
95-
if r not in mp:
96-
mp[r] = i
97106
return False
98107
```
99108

@@ -102,18 +111,16 @@ class Solution:
102111
```java
103112
class Solution {
104113
public boolean checkSubarraySum(int[] nums, int k) {
105-
Map<Integer, Integer> mp = new HashMap<>();
106-
mp.put(0, -1);
114+
Map<Integer, Integer> d = new HashMap<>();
115+
d.put(0, -1);
107116
int s = 0;
108117
for (int i = 0; i < nums.length; ++i) {
109-
s += nums[i];
110-
int r = s % k;
111-
if (mp.containsKey(r) && i - mp.get(r) >= 2) {
118+
s = (s + nums[i]) % k;
119+
if (!d.containsKey(s)) {
120+
d.put(s, i);
121+
} else if (i - d.get(s) > 1) {
112122
return true;
113123
}
114-
if (!mp.containsKey(r)) {
115-
mp.put(r, i);
116-
}
117124
}
118125
return false;
119126
}
@@ -126,14 +133,15 @@ class Solution {
126133
class Solution {
127134
public:
128135
bool checkSubarraySum(vector<int>& nums, int k) {
129-
unordered_map<int, int> mp;
130-
mp[0] = -1;
136+
unordered_map<int, int> d{{0, -1}};
131137
int s = 0;
132138
for (int i = 0; i < nums.size(); ++i) {
133-
s += nums[i];
134-
int r = s % k;
135-
if (mp.count(r) && i - mp[r] >= 2) return true;
136-
if (!mp.count(r)) mp[r] = i;
139+
s = (s + nums[i]) % k;
140+
if (!d.contains(s)) {
141+
d[s] = i;
142+
} else if (i - d[s] > 1) {
143+
return true;
144+
}
137145
}
138146
return false;
139147
}
@@ -144,17 +152,15 @@ public:
144152
145153
```go
146154
func checkSubarraySum(nums []int, k int) bool {
147-
mp := map[int]int{0: -1}
155+
d := map[int]int{0: -1}
148156
s := 0
149-
for i, v := range nums {
150-
s += v
151-
r := s % k
152-
if j, ok := mp[r]; ok && i-j >= 2 {
157+
for i, x := range nums {
158+
s = (s + x) % k
159+
if _, ok := d[s]; !ok {
160+
d[s] = i
161+
} else if i-d[s] > 1 {
153162
return true
154163
}
155-
if _, ok := mp[r]; !ok {
156-
mp[r] = i
157-
}
158164
}
159165
return false
160166
}
@@ -164,19 +170,16 @@ func checkSubarraySum(nums []int, k int) bool {
164170

165171
```ts
166172
function checkSubarraySum(nums: number[], k: number): boolean {
167-
const n = nums.length;
168-
let prefixSum = 0;
169-
const map = new Map([[0, 0]]);
170-
171-
for (let i = 0; i < n; i++) {
172-
prefixSum += nums[i];
173-
const remainder = prefixSum % k;
174-
175-
if (!map.has(remainder)) {
176-
map.set(remainder, i + 1);
177-
} else if (i - map.get(remainder)! > 0) return true;
173+
const d: Record<number, number> = { 0: -1 };
174+
let s = 0;
175+
for (let i = 0; i < nums.length; ++i) {
176+
s = (s + nums[i]) % k;
177+
if (!d.hasOwnProperty(s)) {
178+
d[s] = i;
179+
} else if (i - d[s] > 1) {
180+
return true;
181+
}
178182
}
179-
180183
return false;
181184
}
182185
```

solution/0500-0599/0523.Continuous Subarray Sum/Solution.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution {
22
public:
33
bool checkSubarraySum(vector<int>& nums, int k) {
4-
unordered_map<int, int> mp;
5-
mp[0] = -1;
4+
unordered_map<int, int> d{{0, -1}};
65
int s = 0;
76
for (int i = 0; i < nums.size(); ++i) {
8-
s += nums[i];
9-
int r = s % k;
10-
if (mp.count(r) && i - mp[r] >= 2) return true;
11-
if (!mp.count(r)) mp[r] = i;
7+
s = (s + nums[i]) % k;
8+
if (!d.contains(s)) {
9+
d[s] = i;
10+
} else if (i - d[s] > 1) {
11+
return true;
12+
}
1213
}
1314
return false;
1415
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
func checkSubarraySum(nums []int, k int) bool {
2-
mp := map[int]int{0: -1}
2+
d := map[int]int{0: -1}
33
s := 0
4-
for i, v := range nums {
5-
s += v
6-
r := s % k
7-
if j, ok := mp[r]; ok && i-j >= 2 {
4+
for i, x := range nums {
5+
s = (s + x) % k
6+
if _, ok := d[s]; !ok {
7+
d[s] = i
8+
} else if i-d[s] > 1 {
89
return true
910
}
10-
if _, ok := mp[r]; !ok {
11-
mp[r] = i
12-
}
1311
}
1412
return false
1513
}

solution/0500-0599/0523.Continuous Subarray Sum/Solution.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
22
public boolean checkSubarraySum(int[] nums, int k) {
3-
Map<Integer, Integer> mp = new HashMap<>();
4-
mp.put(0, -1);
3+
Map<Integer, Integer> d = new HashMap<>();
4+
d.put(0, -1);
55
int s = 0;
66
for (int i = 0; i < nums.length; ++i) {
7-
s += nums[i];
8-
int r = s % k;
9-
if (mp.containsKey(r) && i - mp.get(r) >= 2) {
7+
s = (s + nums[i]) % k;
8+
if (!d.containsKey(s)) {
9+
d.put(s, i);
10+
} else if (i - d.get(s) > 1) {
1011
return true;
1112
}
12-
if (!mp.containsKey(r)) {
13-
mp.put(r, i);
14-
}
1513
}
1614
return false;
1715
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution:
22
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
3+
d = {0: -1}
34
s = 0
4-
mp = {0: -1}
5-
for i, v in enumerate(nums):
6-
s += v
7-
r = s % k
8-
if r in mp and i - mp[r] >= 2:
5+
for i, x in enumerate(nums):
6+
s = (s + x) % k
7+
if s not in d:
8+
d[s] = i
9+
elif i - d[s] > 1:
910
return True
10-
if r not in mp:
11-
mp[r] = i
1211
return False

0 commit comments

Comments
 (0)