Skip to content
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 @@ -77,25 +77,154 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3729.Co
#### Python3

```python

class Solution:
def numGoodSubarrays(self, nums: List[int], k: int) -> int:
cnt = Counter({0: 1})
ans = s = 0
for x in nums:
s = (s + x) % k
ans += cnt[s]
cnt[s] += 1
n = len(nums)
i = 0
while i < n:
j = i + 1
while j < n and nums[j] == nums[i]:
j += 1
m = j - i
for h in range(1, m + 1):
if (h * nums[i]) % k == 0:
ans -= m - h
i = j
return ans
```

#### Java

```java

class Solution {
public long numGoodSubarrays(int[] nums, int k) {
long ans = 0;
int s = 0;
Map<Integer, Integer> cnt = new HashMap<>();
cnt.put(0, 1);
for (int x : nums) {
s = (s + x) % k;
ans += cnt.getOrDefault(s, 0);
cnt.merge(s, 1, Integer::sum);
}
int n = nums.length;
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && nums[j] == nums[i]) {
++j;
}
int m = j - i;
for (int h = 1; h <= m; ++h) {
if (1L * nums[i] * h % k == 0) {
ans -= (m - h);
}
}
i = j;
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
long long numGoodSubarrays(vector<int>& nums, int k) {
long long ans = 0;
int s = 0;
unordered_map<int, int> cnt;
cnt[0] = 1;
for (int x : nums) {
s = (s + x) % k;
ans += cnt[s]++;
}
int n = nums.size();
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && nums[j] == nums[i]) {
++j;
}
int m = j - i;
for (int h = 1; h <= m; ++h) {
if (1LL * nums[i] * h % k == 0) {
ans -= (m - h);
}
}
i = j;
}
return ans;
}
};
```

#### Go

```go
func numGoodSubarrays(nums []int, k int) (ans int64) {
s := 0
cnt := map[int]int{0: 1}
for _, x := range nums {
s = (s + x) % k
ans += int64(cnt[s])
cnt[s]++
}

n := len(nums)
for i := 0; i < n; {
j := i + 1
for j < n && nums[j] == nums[i] {
j++
}
m := j - i
for h := 1; h <= m; h++ {
if int64(nums[i])*int64(h)%int64(k) == 0 {
ans -= int64(m - h)
}
}
i = j
}
return
}
```

#### TypeScript

```ts
function numGoodSubarrays(nums: number[], k: number): number {
let ans = 0;
let s = 0;
const cnt = new Map<number, number>();
cnt.set(0, 1);

for (const x of nums) {
s = (s + x) % k;
ans += cnt.get(s) ?? 0;
cnt.set(s, (cnt.get(s) ?? 0) + 1);
}

const n = nums.length;
for (let i = 0; i < n; ) {
let j = i + 1;
while (j < n && nums[j] === nums[i]) ++j;
const m = j - i;
for (let h = 1; h <= m; ++h) {
if ((nums[i] * h) % k === 0) {
ans -= m - h;
}
}
i = j;
}

return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,154 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3729.Co
#### Python3

```python

class Solution:
def numGoodSubarrays(self, nums: List[int], k: int) -> int:
cnt = Counter({0: 1})
ans = s = 0
for x in nums:
s = (s + x) % k
ans += cnt[s]
cnt[s] += 1
n = len(nums)
i = 0
while i < n:
j = i + 1
while j < n and nums[j] == nums[i]:
j += 1
m = j - i
for h in range(1, m + 1):
if (h * nums[i]) % k == 0:
ans -= m - h
i = j
return ans
```

#### Java

```java

class Solution {
public long numGoodSubarrays(int[] nums, int k) {
long ans = 0;
int s = 0;
Map<Integer, Integer> cnt = new HashMap<>();
cnt.put(0, 1);
for (int x : nums) {
s = (s + x) % k;
ans += cnt.getOrDefault(s, 0);
cnt.merge(s, 1, Integer::sum);
}
int n = nums.length;
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && nums[j] == nums[i]) {
++j;
}
int m = j - i;
for (int h = 1; h <= m; ++h) {
if (1L * nums[i] * h % k == 0) {
ans -= (m - h);
}
}
i = j;
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
long long numGoodSubarrays(vector<int>& nums, int k) {
long long ans = 0;
int s = 0;
unordered_map<int, int> cnt;
cnt[0] = 1;
for (int x : nums) {
s = (s + x) % k;
ans += cnt[s]++;
}
int n = nums.size();
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && nums[j] == nums[i]) {
++j;
}
int m = j - i;
for (int h = 1; h <= m; ++h) {
if (1LL * nums[i] * h % k == 0) {
ans -= (m - h);
}
}
i = j;
}
return ans;
}
};
```

#### Go

```go
func numGoodSubarrays(nums []int, k int) (ans int64) {
s := 0
cnt := map[int]int{0: 1}
for _, x := range nums {
s = (s + x) % k
ans += int64(cnt[s])
cnt[s]++
}

n := len(nums)
for i := 0; i < n; {
j := i + 1
for j < n && nums[j] == nums[i] {
j++
}
m := j - i
for h := 1; h <= m; h++ {
if int64(nums[i])*int64(h)%int64(k) == 0 {
ans -= int64(m - h)
}
}
i = j
}
return
}
```

#### TypeScript

```ts
function numGoodSubarrays(nums: number[], k: number): number {
let ans = 0;
let s = 0;
const cnt = new Map<number, number>();
cnt.set(0, 1);

for (const x of nums) {
s = (s + x) % k;
ans += cnt.get(s) ?? 0;
cnt.set(s, (cnt.get(s) ?? 0) + 1);
}

const n = nums.length;
for (let i = 0; i < n; ) {
let j = i + 1;
while (j < n && nums[j] === nums[i]) ++j;
const m = j - i;
for (let h = 1; h <= m; ++h) {
if ((nums[i] * h) % k === 0) {
ans -= m - h;
}
}
i = j;
}

return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
long long numGoodSubarrays(vector<int>& nums, int k) {
long long ans = 0;
int s = 0;
unordered_map<int, int> cnt;
cnt[0] = 1;
for (int x : nums) {
s = (s + x) % k;
ans += cnt[s]++;
}
int n = nums.size();
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && nums[j] == nums[i]) {
++j;
}
int m = j - i;
for (int h = 1; h <= m; ++h) {
if (1LL * nums[i] * h % k == 0) {
ans -= (m - h);
}
}
i = j;
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
func numGoodSubarrays(nums []int, k int) (ans int64) {
s := 0
cnt := map[int]int{0: 1}
for _, x := range nums {
s = (s + x) % k
ans += int64(cnt[s])
cnt[s]++
}

n := len(nums)
for i := 0; i < n; {
j := i + 1
for j < n && nums[j] == nums[i] {
j++
}
m := j - i
for h := 1; h <= m; h++ {
if int64(nums[i])*int64(h)%int64(k) == 0 {
ans -= int64(m - h)
}
}
i = j
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public long numGoodSubarrays(int[] nums, int k) {
long ans = 0;
int s = 0;
Map<Integer, Integer> cnt = new HashMap<>();
cnt.put(0, 1);
for (int x : nums) {
s = (s + x) % k;
ans += cnt.getOrDefault(s, 0);
cnt.merge(s, 1, Integer::sum);
}
int n = nums.length;
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && nums[j] == nums[i]) {
++j;
}
int m = j - i;
for (int h = 1; h <= m; ++h) {
if (1L * nums[i] * h % k == 0) {
ans -= (m - h);
}
}
i = j;
}
return ans;
}
}
Loading