1365. 有多少小于当前数字的数字『简单』

题目来源于力扣(LeetCode

一、题目

1365. 有多少小于当前数字的数字

提示:

  • 2 <= nums.length <= 500

  • 0 <= nums[i] <= 100

二、解题思路

2.1 暴力法

  1. 创建一个与 nums 相同长度的数组,用于存储相同索引上 nums 数组中元素大于其他元素的次数

  2. 将 nums 数组中每一个元素都与数组中的其他元素进行比较

2.2 桶思想

  1. 通过给定的数组元素取值范围,定义一个元素取值范围的数组,用于存储元素出现的次数

  2. 从前往后遍历元素,则所遍历的元素是从小到大的

  3. 后面的元素即是大于前面元素的数字,通过加上数组上的元素即可得到小于当前数字的元素所出现的次数,并将结果赋值到该索引的元素上

  4. 遍历 nums 数组,此时通过 nums 数组上的元素即可对应到 bucket 数组中bucket 索引上的元素即为该索引值所大于的 nums 数组元素的次数

三、代码实现

3.1 暴力法

public static int[] smallerNumbersThanCurrent(int[] nums) {
    int[] res = new int[nums.length];
    // nums 数组中每一个元素都与数组中的其他元素进行比较,统计出小于该元素的元素个数
    for (int i = 0; i < nums.length; i++) {
        for (int j = 0; j < nums.length; j++) {
            if (i == j) {
                continue;
            }
            // 其他元素小于该元素时,统计加 1
            if (nums[j] < nums[i]) {
                res[i] ++;
            }
        }
    }
    return res;
}

3.2 桶思想

public static int[] smallerNumbersThanCurrent(int[] nums) {
    // 桶思想,因为给定的数组元素的范围是 0 <= nums[i] <= 100,所以可以用数组记录下每个元素出现的次数
    int[] bucket = new int[101];

    for (int i : nums) {
        bucket[i]++;
    }
    // 定义count:记录下数组中全部元素出现的次数
    int count = 0;

    for (int i = 0; i < bucket.length; i++) {
        if (bucket[i] > 0) {
            // 定义临时变量进行替换操作
            int temp = bucket[i];
            // 关键:当前所遍历的索引上的元素为 count 值,即小于该元素的元素个数
            bucket[i] = count;
            // count 是加上当前索引上的值,即元素在数组中出现的次数
            count += temp;
        }
    }

    for (int i = 0; i < nums.length; i++) {
        int j = nums[i];
        // bucket 数组中的元素已经记录下了 索引位中的元素在 nums 数组中出现的次数
        nums[i] = bucket[j];
    }
    return nums;
}

四、执行用时

4.1 暴力法

4.2 桶思想

五、部分测试用例

public static void main(String[] args) {
    int[] nums = {8, 1, 2, 2, 3};  // output:{4, 0, 1, 1, 3}
//        int[] nums = {6, 5, 4, 8};  // output:{2, 1, 0, 3}
//        int[] nums = {7, 7, 7, 7};  // output:{0, 0, 0, 0}
    int[] result = smallerNumbersThanCurrent2(nums);
    System.out.println(Arrays.toString(result));
}
原文地址:https://www.cnblogs.com/zhiyin1209/p/12878723.html