Count Elements With Strictly Smaller and Greater Elements
给一个数组, 问这个数组有多少个数字是至少有一个数字严格比它大还有一个严格比它小的.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Solution { public int countElements(int[] nums) { int nMax = 0; int max = Integer.MIN_VALUE; int nMin = 0; int min = Integer.MAX_VALUE; for(int n : nums){ max = Math.max(max, n); min = Math.min(min, n); } for(int n : nums){ if(n == max) nMax++; if(n == min) nMin++; } if(nMax == nums.length) return 0; return nums.length - nMax - nMin; } } |