diff --git a/leetcode/3101-3200/3151.Special-Array-I/README.md b/leetcode/3101-3200/3151.Special-Array-I/README.md index 2b49c31d7..8bd523014 100755 --- a/leetcode/3101-3200/3151.Special-Array-I/README.md +++ b/leetcode/3101-3200/3151.Special-Array-I/README.md @@ -1,28 +1,45 @@ # [3151.Special Array I][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity. + +You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1] + +Output: true + +Explanation: + +There is only one element. So the answer is true. +``` + +**Example 2:** + ``` +Input: nums = [2,1,4] + +Output: true -## 题意 -> ... +Explanation: + +There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true. +``` -## 题解 +**Example 3:** -### 思路1 -> ... -Special Array I -```go ``` +Input: nums = [4,3,1,6] +Output: false + +Explanation: + +nums[1] and nums[2] are both odd. So the answer is false. +``` ## 结语 diff --git a/leetcode/3101-3200/3151.Special-Array-I/Solution.go b/leetcode/3101-3200/3151.Special-Array-I/Solution.go index d115ccf5e..a79b7843c 100644 --- a/leetcode/3101-3200/3151.Special-Array-I/Solution.go +++ b/leetcode/3101-3200/3151.Special-Array-I/Solution.go @@ -1,5 +1,13 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) bool { + if len(nums) == 1 { + return true + } + for i := 0; i < len(nums)-1; i++ { + if nums[i]&1 == nums[i+1]&1 { + return false + } + } + return true } diff --git a/leetcode/3101-3200/3151.Special-Array-I/Solution_test.go b/leetcode/3101-3200/3151.Special-Array-I/Solution_test.go index 14ff50eb4..75bbeca27 100644 --- a/leetcode/3101-3200/3151.Special-Array-I/Solution_test.go +++ b/leetcode/3101-3200/3151.Special-Array-I/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs []int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1}, true}, + {"TestCase2", []int{2, 1, 4}, true}, + {"TestCase3", []int{4, 3, 1, 6}, false}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }