570.Count Positive and Negative Numbers in an Array
Easy
Given an array
nums, return [positives, negatives, zeros]: how many elements are greater than 0, less than 0, and exactly 0.
Classifying a single number by its sign is a conditionals exercise; doing it for a whole array adds a loop and three counters. Zero is the case that is easiest to forget — it is neither positive nor negativeSign of ZeroZero is the boundary between positive and negative numbers and belongs to neither group. — and the second solution shows it does not even need its own branch.
Example 1:
Input: nums = [3,-1,0,8,-6]
Output: [2,2,1]
Example 2:
Input: nums = [0,0]
Output: [0,0,2]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i] ≤ 1000000
nums =
[3, -1, 0, 8, -6]