Skip to content

Commit cc079a6

Browse files
authored
Merge pull request #1109 from 0xff-dev/3151
Add solution and test-cases for problem 3151
2 parents f8522c2 + 628e46d commit cc079a6

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

leetcode/3101-3200/3151.Special-Array-I/README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3151.Special Array I][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+
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.
5+
6+
You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1]
12+
13+
Output: true
14+
15+
Explanation:
16+
17+
There is only one element. So the answer is true.
18+
```
19+
20+
**Example 2:**
21+
1322
```
23+
Input: nums = [2,1,4]
24+
25+
Output: true
1426
15-
## 题意
16-
> ...
27+
Explanation:
28+
29+
There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.
30+
```
1731

18-
## 题解
32+
**Example 3:**
1933

20-
### 思路1
21-
> ...
22-
Special Array I
23-
```go
2434
```
35+
Input: nums = [4,3,1,6]
2536
37+
Output: false
38+
39+
Explanation:
40+
41+
nums[1] and nums[2] are both odd. So the answer is false.
42+
```
2643

2744
## 结语
2845

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) bool {
4+
if len(nums) == 1 {
5+
return true
6+
}
7+
for i := 0; i < len(nums)-1; i++ {
8+
if nums[i]&1 == nums[i+1]&1 {
9+
return false
10+
}
11+
}
12+
return true
513
}

leetcode/3101-3200/3151.Special-Array-I/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1}, true},
17+
{"TestCase2", []int{2, 1, 4}, true},
18+
{"TestCase3", []int{4, 3, 1, 6}, false},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)