-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1365.java
More file actions
38 lines (32 loc) · 1.2 KB
/
LeetCode1365.java
File metadata and controls
38 lines (32 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Arrays;
public class LeetCode1365 {
public static void main(String[] args) {
// 输入:nums = [8,1,2,2,3]
// 输出:[4,0,1,1,3]
System.out.println(Arrays.toString(new Solution1365().smallerNumbersThanCurrent(new int[] { 8, 1, 2, 2, 3 })));
// 输入:nums = [6,5,4,8]
// 输出:[2,1,0,3]
System.out.println(Arrays.toString(new Solution1365().smallerNumbersThanCurrent(new int[] { 6, 5, 4, 8 })));
// 输入:nums = [7,7,7,7]
// 输出:[0,0,0,0]
System.out.println(Arrays.toString(new Solution1365().smallerNumbersThanCurrent(new int[] { 7, 7, 7, 7 })));
}
}
class Solution1365 {
public int[] smallerNumbersThanCurrent(int[] nums) {
int[] counts = new int[101];
for (int i = 0; i < nums.length; i++) {
counts[nums[i]]++;
}
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum = 0;
// 这一步可以用一个累加来做 count[i+1] = count[i] + count[i+1]
for (int value = 0; value < nums[i]; value++) {
sum += counts[value];
}
nums[i] = sum;
}
return nums;
}
}