Skip to content

feat: update lc problems #3115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ tags:
<p><strong>示例&nbsp;1:</strong></p>

<pre>
<strong>输入:</strong>nums = [2<code>,5,6,0,0,1,2]</code>, target = 0
<strong>输入:</strong>nums = <code>[2,5,6,0,0,1,2]</code>, target = 0
<strong>输出:</strong>true
</pre>

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

<pre>
<strong>输入:</strong>nums = [2<code>,5,6,0,0,1,2]</code>, target = 3
<strong>输入:</strong>nums = <code>[2,5,6,0,0,1,2]</code>, target = 3
<strong>输出:</strong>false</pre>

<p>&nbsp;</p>
Expand Down
87 changes: 45 additions & 42 deletions solution/0500-0599/0523.Continuous Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:前缀和 + 哈希表

根据题目描述,如果存在两个前缀和模 $k$ 的余数相同的位置 $i$ 和 $j$(不妨设 $j < i$),那么 $\text{nums}[j+1..i]$ 这个子数组的和是 $k$ 的倍数。

因此,我们可以使用哈希表存储每个前缀和模 $k$ 的余数第一次出现的位置。初始时,我们在哈希表中存入一对键值对 $(0, -1)$,表示前缀和为 $0$ 的余数 $0$ 出现在位置 $-1$。

遍历数组时,我们计算当前前缀和的模 $k$ 的余数,如果当前前缀和的模 $k$ 的余数没有在哈希表中出现过,我们就将当前前缀和的模 $k$ 的余数和对应的位置存入哈希表中。否则,如果当前前缀和的模 $k$ 的余数在哈希表中已经出现过,位置为 $j$,那么我们就找到了一个满足条件的子数组 $\text{nums}[j+1..i]$,因此返回 $\text{True}$。

遍历结束后,如果没有找到满足条件的子数组,我们返回 $\text{False}$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\text{nums}$ 的长度。

<!-- tabs:start -->

Expand All @@ -86,15 +96,14 @@ tags:
```python
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
d = {0: -1}
s = 0
mp = {0: -1}
for i, v in enumerate(nums):
s += v
r = s % k
if r in mp and i - mp[r] >= 2:
for i, x in enumerate(nums):
s = (s + x) % k
if s not in d:
d[s] = i
elif i - d[s] > 1:
return True
if r not in mp:
mp[r] = i
return False
```

Expand All @@ -103,18 +112,16 @@ class Solution:
```java
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> mp = new HashMap<>();
mp.put(0, -1);
Map<Integer, Integer> d = new HashMap<>();
d.put(0, -1);
int s = 0;
for (int i = 0; i < nums.length; ++i) {
s += nums[i];
int r = s % k;
if (mp.containsKey(r) && i - mp.get(r) >= 2) {
s = (s + nums[i]) % k;
if (!d.containsKey(s)) {
d.put(s, i);
} else if (i - d.get(s) > 1) {
return true;
}
if (!mp.containsKey(r)) {
mp.put(r, i);
}
}
return false;
}
Expand All @@ -127,14 +134,15 @@ class Solution {
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = -1;
unordered_map<int, int> d{{0, -1}};
int s = 0;
for (int i = 0; i < nums.size(); ++i) {
s += nums[i];
int r = s % k;
if (mp.count(r) && i - mp[r] >= 2) return true;
if (!mp.count(r)) mp[r] = i;
s = (s + nums[i]) % k;
if (!d.contains(s)) {
d[s] = i;
} else if (i - d[s] > 1) {
return true;
}
}
return false;
}
Expand All @@ -145,17 +153,15 @@ public:

```go
func checkSubarraySum(nums []int, k int) bool {
mp := map[int]int{0: -1}
d := map[int]int{0: -1}
s := 0
for i, v := range nums {
s += v
r := s % k
if j, ok := mp[r]; ok && i-j >= 2 {
for i, x := range nums {
s = (s + x) % k
if _, ok := d[s]; !ok {
d[s] = i
} else if i-d[s] > 1 {
return true
}
if _, ok := mp[r]; !ok {
mp[r] = i
}
}
return false
}
Expand All @@ -165,19 +171,16 @@ func checkSubarraySum(nums []int, k int) bool {

```ts
function checkSubarraySum(nums: number[], k: number): boolean {
const n = nums.length;
let prefixSum = 0;
const map = new Map([[0, 0]]);

for (let i = 0; i < n; i++) {
prefixSum += nums[i];
const remainder = prefixSum % k;

if (!map.has(remainder)) {
map.set(remainder, i + 1);
} else if (i - map.get(remainder)! > 0) return true;
const d: Record<number, number> = { 0: -1 };
let s = 0;
for (let i = 0; i < nums.length; ++i) {
s = (s + nums[i]) % k;
if (!d.hasOwnProperty(s)) {
d[s] = i;
} else if (i - d[s] > 1) {
return true;
}
}

return false;
}
```
Expand Down
87 changes: 45 additions & 42 deletions solution/0500-0599/0523.Continuous Subarray Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,17 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Prefix Sum + Hash Table

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$.

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$.

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}$.

After completing the iteration, if no subarray meeting the conditions is found, we return $\text{False}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.

<!-- tabs:start -->

Expand All @@ -85,15 +95,14 @@ tags:
```python
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
d = {0: -1}
s = 0
mp = {0: -1}
for i, v in enumerate(nums):
s += v
r = s % k
if r in mp and i - mp[r] >= 2:
for i, x in enumerate(nums):
s = (s + x) % k
if s not in d:
d[s] = i
elif i - d[s] > 1:
return True
if r not in mp:
mp[r] = i
return False
```

Expand All @@ -102,18 +111,16 @@ class Solution:
```java
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> mp = new HashMap<>();
mp.put(0, -1);
Map<Integer, Integer> d = new HashMap<>();
d.put(0, -1);
int s = 0;
for (int i = 0; i < nums.length; ++i) {
s += nums[i];
int r = s % k;
if (mp.containsKey(r) && i - mp.get(r) >= 2) {
s = (s + nums[i]) % k;
if (!d.containsKey(s)) {
d.put(s, i);
} else if (i - d.get(s) > 1) {
return true;
}
if (!mp.containsKey(r)) {
mp.put(r, i);
}
}
return false;
}
Expand All @@ -126,14 +133,15 @@ class Solution {
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = -1;
unordered_map<int, int> d{{0, -1}};
int s = 0;
for (int i = 0; i < nums.size(); ++i) {
s += nums[i];
int r = s % k;
if (mp.count(r) && i - mp[r] >= 2) return true;
if (!mp.count(r)) mp[r] = i;
s = (s + nums[i]) % k;
if (!d.contains(s)) {
d[s] = i;
} else if (i - d[s] > 1) {
return true;
}
}
return false;
}
Expand All @@ -144,17 +152,15 @@ public:

```go
func checkSubarraySum(nums []int, k int) bool {
mp := map[int]int{0: -1}
d := map[int]int{0: -1}
s := 0
for i, v := range nums {
s += v
r := s % k
if j, ok := mp[r]; ok && i-j >= 2 {
for i, x := range nums {
s = (s + x) % k
if _, ok := d[s]; !ok {
d[s] = i
} else if i-d[s] > 1 {
return true
}
if _, ok := mp[r]; !ok {
mp[r] = i
}
}
return false
}
Expand All @@ -164,19 +170,16 @@ func checkSubarraySum(nums []int, k int) bool {

```ts
function checkSubarraySum(nums: number[], k: number): boolean {
const n = nums.length;
let prefixSum = 0;
const map = new Map([[0, 0]]);

for (let i = 0; i < n; i++) {
prefixSum += nums[i];
const remainder = prefixSum % k;

if (!map.has(remainder)) {
map.set(remainder, i + 1);
} else if (i - map.get(remainder)! > 0) return true;
const d: Record<number, number> = { 0: -1 };
let s = 0;
for (let i = 0; i < nums.length; ++i) {
s = (s + nums[i]) % k;
if (!d.hasOwnProperty(s)) {
d[s] = i;
} else if (i - d[s] > 1) {
return true;
}
}

return false;
}
```
Expand Down
13 changes: 7 additions & 6 deletions solution/0500-0599/0523.Continuous Subarray Sum/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = -1;
unordered_map<int, int> d{{0, -1}};
int s = 0;
for (int i = 0; i < nums.size(); ++i) {
s += nums[i];
int r = s % k;
if (mp.count(r) && i - mp[r] >= 2) return true;
if (!mp.count(r)) mp[r] = i;
s = (s + nums[i]) % k;
if (!d.contains(s)) {
d[s] = i;
} else if (i - d[s] > 1) {
return true;
}
}
return false;
}
Expand Down
14 changes: 6 additions & 8 deletions solution/0500-0599/0523.Continuous Subarray Sum/Solution.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
func checkSubarraySum(nums []int, k int) bool {
mp := map[int]int{0: -1}
d := map[int]int{0: -1}
s := 0
for i, v := range nums {
s += v
r := s % k
if j, ok := mp[r]; ok && i-j >= 2 {
for i, x := range nums {
s = (s + x) % k
if _, ok := d[s]; !ok {
d[s] = i
} else if i-d[s] > 1 {
return true
}
if _, ok := mp[r]; !ok {
mp[r] = i
}
}
return false
}
14 changes: 6 additions & 8 deletions solution/0500-0599/0523.Continuous Subarray Sum/Solution.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> mp = new HashMap<>();
mp.put(0, -1);
Map<Integer, Integer> d = new HashMap<>();
d.put(0, -1);
int s = 0;
for (int i = 0; i < nums.length; ++i) {
s += nums[i];
int r = s % k;
if (mp.containsKey(r) && i - mp.get(r) >= 2) {
s = (s + nums[i]) % k;
if (!d.containsKey(s)) {
d.put(s, i);
} else if (i - d.get(s) > 1) {
return true;
}
if (!mp.containsKey(r)) {
mp.put(r, i);
}
}
return false;
}
Expand Down
13 changes: 6 additions & 7 deletions solution/0500-0599/0523.Continuous Subarray Sum/Solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
d = {0: -1}
s = 0
mp = {0: -1}
for i, v in enumerate(nums):
s += v
r = s % k
if r in mp and i - mp[r] >= 2:
for i, x in enumerate(nums):
s = (s + x) % k
if s not in d:
d[s] = i
elif i - d[s] > 1:
return True
if r not in mp:
mp[r] = i
return False
Loading
Loading