-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 3 MB (100…
….00%)
- Loading branch information
1 parent
0e96bdd
commit f56807d
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<p>An array is considered <strong>special</strong> if every pair of its adjacent elements contains two numbers with different parity.<!-- notionvc: e6bed0fa-c67d-43a7-81b4-99fb85b99e98 --></p> | ||
|
||
<p>You are given an array of integers <code>nums</code>. Return <code>true</code> if <code>nums</code> is a <strong>special</strong> array, otherwise, return <code>false</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">true</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>There is only one element. So the answer is <code>true</code>.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,4]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">true</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>There is only two pairs: <code>(2,1)</code> and <code>(1,4)</code>, and both of them contain numbers with different parity. So the answer is <code>true</code>.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,1,6]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">false</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p><code>nums[1]</code> and <code>nums[2]</code> are both odd. So the answer is <code>false</code>.</p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
func isArraySpecial(nums []int) bool { | ||
prev := -1 | ||
|
||
for _, n := range nums { | ||
if prev != -1 && n % 2 == prev % 2 { | ||
return false | ||
} | ||
prev = n | ||
} | ||
|
||
return true | ||
} |