Skip to content

Commit dee5cd0

Browse files
committed
feat: add solutions to lc problem: No.0974
No.0974.Subarrays Sums Divisible by K
1 parent 58c87af commit dee5cd0

File tree

6 files changed

+172
-2
lines changed

6 files changed

+172
-2
lines changed

solution/0900-0999/0974.Subarray Sums Divisible by K/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,84 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
前缀和 + 哈希表。
37+
38+
注意:不同的语言负数取模的值不一定相同,有的语言为负数,对于这种情况需要特殊处理。
39+
3640
<!-- tabs:start -->
3741

3842
### **Python3**
3943

4044
<!-- 这里可写当前语言的特殊实现逻辑 -->
4145

4246
```python
43-
47+
class Solution:
48+
def subarraysDivByK(self, nums: List[int], k: int) -> int:
49+
ans = s = 0
50+
counter = Counter({0: 1})
51+
for num in nums:
52+
s += num
53+
ans += counter[s % k]
54+
counter[s % k] += 1
55+
return ans
4456
```
4557

4658
### **Java**
4759

4860
<!-- 这里可写当前语言的特殊实现逻辑 -->
4961

5062
```java
63+
class Solution {
64+
public int subarraysDivByK(int[] nums, int k) {
65+
Map<Integer, Integer> counter = new HashMap<>();
66+
counter.put(0, 1);
67+
int s = 0, ans = 0;
68+
for (int num : nums) {
69+
s += num;
70+
int t = (s % k + k) % k;
71+
ans += counter.getOrDefault(t, 0);
72+
counter.put(t, counter.getOrDefault(t, 0) + 1);
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int subarraysDivByK(vector<int>& nums, int k) {
85+
unordered_map<int, int> counter;
86+
counter[0] = 1;
87+
int s = 0, ans = 0;
88+
for (int& num : nums)
89+
{
90+
s += num;
91+
int t = (s % k + k) % k;
92+
ans += counter[t];
93+
++counter[t];
94+
}
95+
return ans;
96+
}
97+
};
98+
```
5199
100+
### **Go**
101+
102+
```go
103+
func subarraysDivByK(nums []int, k int) int {
104+
counter := map[int]int{0: 1}
105+
ans, s := 0, 0
106+
for _, num := range nums {
107+
s += num
108+
t := (s%k + k) % k
109+
ans += counter[t]
110+
counter[t]++
111+
}
112+
return ans
113+
}
52114
```
53115

54116
### **...**

solution/0900-0999/0974.Subarray Sums Divisible by K/README_EN.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,71 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def subarraysDivByK(self, nums: List[int], k: int) -> int:
48+
ans = s = 0
49+
counter = Counter({0: 1})
50+
for num in nums:
51+
s += num
52+
ans += counter[s % k]
53+
counter[s % k] += 1
54+
return ans
4755
```
4856

4957
### **Java**
5058

5159
```java
60+
class Solution {
61+
public int subarraysDivByK(int[] nums, int k) {
62+
Map<Integer, Integer> counter = new HashMap<>();
63+
counter.put(0, 1);
64+
int s = 0, ans = 0;
65+
for (int num : nums) {
66+
s += num;
67+
int t = (s % k + k) % k;
68+
ans += counter.getOrDefault(t, 0);
69+
counter.put(t, counter.getOrDefault(t, 0) + 1);
70+
}
71+
return ans;
72+
}
73+
}
74+
```
75+
76+
### **C++**
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
int subarraysDivByK(vector<int>& nums, int k) {
82+
unordered_map<int, int> counter;
83+
counter[0] = 1;
84+
int s = 0, ans = 0;
85+
for (int& num : nums)
86+
{
87+
s += num;
88+
int t = (s % k + k) % k;
89+
ans += counter[t];
90+
++counter[t];
91+
}
92+
return ans;
93+
}
94+
};
95+
```
5296
97+
### **Go**
98+
99+
```go
100+
func subarraysDivByK(nums []int, k int) int {
101+
counter := map[int]int{0: 1}
102+
ans, s := 0, 0
103+
for _, num := range nums {
104+
s += num
105+
t := (s%k + k) % k
106+
ans += counter[t]
107+
counter[t]++
108+
}
109+
return ans
110+
}
53111
```
54112

55113
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int subarraysDivByK(vector<int>& nums, int k) {
4+
unordered_map<int, int> counter;
5+
counter[0] = 1;
6+
int s = 0, ans = 0;
7+
for (int& num : nums)
8+
{
9+
s += num;
10+
int t = (s % k + k) % k;
11+
ans += counter[t];
12+
++counter[t];
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func subarraysDivByK(nums []int, k int) int {
2+
counter := map[int]int{0: 1}
3+
ans, s := 0, 0
4+
for _, num := range nums {
5+
s += num
6+
t := (s%k + k) % k
7+
ans += counter[t]
8+
counter[t]++
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int subarraysDivByK(int[] nums, int k) {
3+
Map<Integer, Integer> counter = new HashMap<>();
4+
counter.put(0, 1);
5+
int s = 0, ans = 0;
6+
for (int num : nums) {
7+
s += num;
8+
int t = (s % k + k) % k;
9+
ans += counter.getOrDefault(t, 0);
10+
counter.put(t, counter.getOrDefault(t, 0) + 1);
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def subarraysDivByK(self, nums: List[int], k: int) -> int:
3+
ans = s = 0
4+
counter = Counter({0: 1})
5+
for num in nums:
6+
s += num
7+
ans += counter[s % k]
8+
counter[s % k] += 1
9+
return ans

0 commit comments

Comments
 (0)