Class Solution
-
- All Implemented Interfaces:
public final class Solution3354 - Make Array Elements Equal to Zero\.
Easy
You are given an integer array
nums.Start by selecting a starting position
currsuch thatnums[curr] == 0, and choose a movement direction of either left or right.After that, you repeat the following process:
If
curris out of the range[0, n - 1], this process ends.If
nums[curr] == 0, move in the current direction by incrementingcurrif you are moving right, or decrementingcurrif you are moving left.Else if
nums[curr] > 0:
A selection of the initial position
currand movement direction is considered valid if every element innumsbecomes 0 by the end of the process.Return the number of possible valid selections.
Example 1:
Input: nums = 1,0,2,0,3
Output: 2
Explanation:
The only possible valid selections are the following:
Choose
curr = 3, and a movement direction to the left.Choose
curr = 3, and a movement direction to the right.
Example 2:
Input: nums = 2,3,4,0,4,1,0
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 100There is at least one element
iwherenums[i] == 0.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegercountValidSelections(IntArray nums)-
-
Method Detail
-
countValidSelections
final Integer countValidSelections(IntArray nums)
-
-
-
-