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 19a838e

Browse files
committed
Merge pull request #398 from 0xff-dev/1248
Add solution and test-cases for problem 1248
2 parents 702d0b1 + 1b906b9 commit 19a838e

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

‎leetcode/1201-1300/1248.Count-Number-of-Nice-Subarrays/README.md‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1248.Count Number of Nice Subarrays][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an array of integers `nums` and an integer `k`. A continuous subarray is called **nice** if there are `k` odd numbers on it.
5+
6+
Return the number of **nice** sub-arrays.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1,1,2,1,1], k = 3
12+
Output: 2
13+
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Number of Nice Subarrays
23-
```go
18+
```
19+
Input: nums = [2,4,6], k = 1
20+
Output: 0
21+
Explanation: There is no odd numbers in the array.
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
28+
Output: 16
29+
```
2630

2731
## 结语
2832

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type tmp1248 struct {
4+
left, right int
5+
}
6+
7+
func Solution(nums []int, k int) int {
8+
ans := 0
9+
l := len(nums)
10+
indies := make([]tmp1248, 0)
11+
even := 0
12+
13+
for i := 0; i < l; i++ {
14+
if nums[i]&1 == 0 {
15+
even++
16+
continue
17+
}
18+
// left can start with itself so, left = even+1
19+
indies = append(indies, tmp1248{left: even + 1})
20+
li := len(indies)
21+
if li > 1 {
22+
indies[li-2].right = even
23+
}
24+
even = 0
25+
}
26+
if len(indies) == 0 || k > len(indies) {
27+
return 0
28+
}
29+
li := len(indies)
30+
indies[li-1].right = even
31+
32+
start := 0
33+
for ; start <= len(indies)-k; start++ {
34+
ans += indies[start].left * (indies[start+k-1].right + 1)
35+
}
36+
return ans
537
}

‎leetcode/1201-1300/1248.Count-Number-of-Nice-Subarrays/Solution_test.go‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,34 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 1, 2, 1, 1}, 3, 2},
18+
{"TestCase2", []int{2, 4, 6}, 1, 0},
19+
{"TestCase3", []int{2, 2, 2, 1, 2, 2, 1, 2, 2, 2}, 2, 16},
20+
{"TestCase4", []int{91473, 45388, 24720, 35841, 29648, 77363, 86290, 58032, 53752, 87188, 34428, 85343, 19801, 73201}, 4, 6},
21+
{"TestCase5", []int{2, 2, 2, 1, 2, 2, 1, 2, 2, 2}, 1, 24},
22+
{"TestCase6", []int{2, 1, 1}, 1, 3},
1923
}
2024

2125
// 开始测试
2226
for i, c := range cases {
2327
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
28+
got := Solution(c.inputs, c.k)
2529
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
31+
c.expect, got, c.inputs, c.k)
2832
}
2933
})
3034
}
3135
}
3236

33-
//压力测试
37+
//压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
//使用案列
41+
//使用案列
3842
func ExampleSolution() {
3943
}

0 commit comments

Comments
(0)

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