LeetCode(Array)1365. How Many Numbers Are Smaller Than the Current Number

淩亂°似流年 2023-09-23 22:40 157阅读 0赞

1.问题

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j’s such that j != i and nums[j] < nums[i].

Return the answer in an array.

Example 1:

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

Example 2:

Input: nums = [6,5,4,8]
Output: [2,1,0,3]

Example 3:

Input: nums = [7,7,7,7]
Output: [0,0,0,0]

Constraints:

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100

2. 解题思路

方法1:

1.新建数组长度和nums相等
2.定义sum,有多少数字小于当前数字
3.for遍历数组,两数据比较,如果nums[i] > nums[j],sum加1,
4.sum的值存入result中
5.添加后sum为0,其他数据再次使用
5.返回result

方法2:

利用桶的思想,数据存放在桶中,然后查询数据,数据的索引值

方法3:

1.复制数组,从小到大排序
2.新建map,将值为key,将键设为value
3.遍历数组,元数据的值和map的值比较,获得map中的value(也就索引值)
4.将索引值存入新建的result数组中
5.返回result

3. 代码

代码1:

  1. class Solution {
  2. public int[] smallerNumbersThanCurrent(int[] nums) {
  3. int[] result = new int[nums.length];//1.新建数组长度和nums相等
  4. int sum = 0;//2.定义sum,有多少数字小于当前数字
  5. for (int i = 0; i < nums.length; i++) {
  6. //3.for遍历数组,两数据比较,如果nums[i] > nums[j],sum加1,
  7. for (int j = 0; j < nums.length; j++) {
  8. if (nums[i] > nums[j]) sum++;
  9. }
  10. result[i] = sum;//4.sum的值存入result中
  11. sum=0;//5.添加后sum为0,其他数据再次使用
  12. }
  13. return result;//6.返回result
  14. }
  15. }

代码和上面代码的解题思路基本相同

  1. class Solution {
  2. public int[] smallerNumbersThanCurrent(int[] nums) {
  3. int[] result = new int[nums.length];
  4. for (int i=0;i<nums.length;i++){
  5. for (int j=0;j<nums.length;j++){
  6. if (nums[i]>nums[j]){
  7. result[i]++;//i本身为0,自增后的数据直接为当前当前和其他数据相比后的个数
  8. }
  9. }
  10. }
  11. return result;
  12. }
  13. }

代码2:

在这里插入图片描述

  1. class Solution {
  2. public int[] smallerNumbersThanCurrent(int[] nums) {
  3. int[] count = new int[101];//1.新建数组count,目的是存值,值为0-100,长度为101
  4. int[] result = new int[nums.length];//2.新建数组result,存储比较后的个数
  5. for (int i =0; i < nums.length; i++) {
  6. //3.for循环遍历,将nums的值存入到count中
  7. count[nums[i]]++;
  8. }
  9. System.out.println(Arrays.toString(count));
  10. for (int i = 1 ; i <= 100; i++) {
  11. //4.for循环遍历,i小于等于100,count的前一个值相加等于本身
  12. count[i] += count[i-1];
  13. }
  14. for (int i = 0; i < nums.length; i++) {
  15. //5.for循环遍历,如果nums的值为0,存入到result中为0,否则result的个数为count[nums的值减去1]
  16. if (nums[i] == 0)
  17. result[i] = 0;
  18. else
  19. result[i] = count[nums[i] - 1];
  20. }
  21. return result;//6.返回result
  22. }
  23. }

解题思路和以上基本相同,不同的是用foreach遍历

  1. class Solution {
  2. public int[] smallerNumbersThanCurrent(int[] nums) {
  3. int[] freq = new int[101];
  4. int[] count = new int[nums.length];
  5. for(int a : nums)
  6. freq[a]++;
  7. for(int i = 1 ; i < freq.length ; i++){
  8. freq[i] += freq[i-1];
  9. }
  10. for(int i = 0 ; i < nums.length ; i++){
  11. if(nums[i]==0)continue;
  12. count[i]=freq[nums[i]-1];
  13. }
  14. return count;
  15. }
  16. }

代码3

在这里插入图片描述

  1. import java.util.Arrays;
  2. import java.util.HashMap;
  3. class Solution {
  4. public int[] smallerNumbersThanCurrent(int[] nums) {
  5. int[] cloneNums = nums.clone();//1.clone数据并从小到大排序
  6. Arrays.sort(cloneNums);//[1, 2, 2, 3, 8]
  7. HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();//2.新建map,如果没有相同的值,存入key为值,value为索引
  8. for(int i = 0; i < cloneNums.length; i++) {
  9. if(!map.containsKey(cloneNums[i])) {
  10. map.put(cloneNums[i], i);//{1=0, 2=1, 3=3, 8=4}
  11. }
  12. }
  13. int[] result = new int[nums.length];//3.新建数组result
  14. for(int i = 0; i < nums.length; i++) {
  15. //4. 遍历数组,取出map中的索引值,并赋值给result
  16. int value = map.get(nums[i]);
  17. result[i] = value;//[4, 0, 1, 1, 3]
  18. }
  19. return result;//5.返回result
  20. }
  21. }

解题思路和以上基本相同

  1. class Solution {
  2. public int[] smallerNumbersThanCurrent(int[] nums) {
  3. int n = nums.length;
  4. int[] copy = Arrays.copyOf(nums, n);//1.创建输入数组的副本。
  5. Arrays.sort(copy);//2.从小到大排序
  6. Map<Integer, Integer> map = new HashMap<>();//3.新建map,排序后的数组,值从大到小排入,将数值为key,i的值为value
  7. for (int i = n - 1; i >= 0; i--) {
  8. map.put(copy[i], i);
  9. }
  10. for (int i = 0; i < n; i++) {
  11. //4.新建map,排序后的数组,值从大到小排入,将数值为key,i的值为value
  12. nums[i] = map.get(nums[i]);//5.从map中取出nums的值,key的个数就是nums的值
  13. }
  14. return nums;//6.返回nums
  15. }
  16. }

发表评论

表情:
评论列表 (有 0 条评论,157人围观)

还没有评论,来说两句吧...

相关阅读