Topic description
Given an array nums
, for each element nums[i]
, please count the number of all numbers in the array that are smaller than it.
换nums[i]
你必须计算出有效的 j 的数量, j
j != i
nums[j] < nums[i]
。
Return the answer as an array.
Example 1:
输入:nums = [8,1,2,2,3]
输出:[4,0,1,1,3]
解释:
对于 nums[0]=8 存在四个比它小的数字:(1,2,2 和 3)。
对于 nums[1]=1 不存在比它小的数字。
对于 nums[2]=2 存在一个比它小的数字:(1)。
对于 nums[3]=2 存在一个比它小的数字:(1)。
对于 nums[4]=3 存在三个比它小的数字:(1,2 和 2)。
Example 2:
输入:nums = [6,5,4,8]
输出:[2,1,0,3]
Example 3:
输入:nums = [7,7,7,7]
输出:[0,0,0,0]
Leetcode original title address: https://leetcode.cn/problems/how-many-numbers-are-smaller-than-the-current-number
idea solution
The problem is not difficult, it is to compare the size and count the number of times. When encountering such a problem, the first thing we meet is the method of brute force loop cracking.
Two-tier loop comparison
In the first layer of loop, get each current item, and then set up another layer of loop to compare whether other items are smaller than the current item in turn.
var smallerNumbersThanCurrent = function (nums) {
// 1. 创建一个和传进来的数组长度一致的数组,且每一项都是0(为0是为了后续统计次数)
let resultArr = (new Array(nums.length)).fill(0)
for (let i = 0; i < nums.length; i++) { // 2. 外层遍历是为了拿到每一项与其余项作比较
let item = nums[i] // 3. 拿到当下项
for (let j = 0; j < nums.length; j++) { // 4. 再循环一次是为了拿到别的项
if (i == j) { // 5. 自身不用比较大小
// console.log('自身跳过不统计次数');
} else {
if (item > nums[j]) { // 6. 若不是自身项,可以比较大小
resultArr[i] = resultArr[i] + 1 // 7. 使用初始创建的0进行次数统计
}
}
}
}
return resultArr
};
Two-layer cycle comparison submission diagram
The index sorted from small to large is the number of times less than the current item
Suppose the array is: [2,1,1,8]
, after sorting it is: [1,1,2,8]
, we will find that the result is: [2,0,0,3]
after sorting Duplicate items only take the index of the first item), because less than that will think of sorting. code show as below:
var smallerNumbersThanCurrent = function (nums) {
// 拷贝一个新的数组,将原有的数组做从小到大排序
let newSortNums = [...nums]
newSortNums.sort((a, b) => {
return a - b
})
// 原有数组的每一项在排序后数组的索引是多少
let resultArr = nums.map((item) => {
return newSortNums.indexOf(item)
})
return resultArr // 即为结果
};
The index sorted from small to large is the number of submissions less than the current item.
Indeed, the efficiency will be better than the double-layer for loop
How to create an array in js for review of knowledge points
Why say this? Because there is such a code in solution 1: let resultArr = (new Array(nums.length)).fill(0)
There are generally the following ways to create an array in js:
1. Create directly, such as: let arr = [111, 222, 3333, 444]
2. Constructor creation, such as creating an empty array (the length is also empty): let arr = new Array()
, create an empty array with a length of 4
, but the value of each item is empty : let arr = new Array(4)
2.1 If you want to fill the value, you can use the fill
method
fill(value,start,end)
method is used to fill the array with data and add items. The parameters must have parametersvalue
, optional parametersstart起始索引
andend终止索引
, which will change the original array and return the modified array
Array.prototype.fill()
mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
new Array(count).fill(initValue)
method is sometimes useful, because sometimes, it is necessary to create something like 动态变化的长度的空数组或统一指定默认值
. In this case, how long the array needs to be, count
is 几
, you need to specify the default value, initValue
Learn a little bit every day, improve a little bit every day.
This is roughly: 老黄牛精神吧
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。