第3题
暴力算法
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int n = nums.length;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (nums[j] < nums[i]) {
cnt++;
}
}
arr[i] = cnt;
}
return arr;
}
}
解法:对于数组中的每一个元素,我们都遍历数组一次,统计小于当前元素的数的数目。