How Many Numbers Are Smaller Than the Current Number
给一个数组, 求一个数组, 里面是原来比数组的数小的个数. 这个题的数只有100那么大, 所以counting一下就好.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Solution { public int[] smallerNumbersThanCurrent(int[] nums) { int[] count = new int[101]; for(int n : nums) count[n] ++; for(int i = 1 ; i < count.length; i++){ count[i] += count[i-1]; } int[] res = new int[nums.length]; for(int i = 0 ; i < nums.length; i++){ if(nums[i] == 0) res[i] = 0; else res[i] = count[nums[i]-1]; } return res; } } |