mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
增加1365. 有多少小于当前数字的数字 JavaScript解法
This commit is contained in:
@ -155,7 +155,23 @@ class Solution:
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
||||
```javascript
|
||||
var smallerNumbersThanCurrent = function(nums) {
|
||||
const map = new Map();// 记录数字 nums[i] 有多少个比它小的数字
|
||||
const res = nums.slice(0);//深拷贝nums
|
||||
res.sort((a,b) => a - b);
|
||||
for(let i = 0; i < res.length; i++){
|
||||
if(!map.has(res[i])){// 遇到了相同的数字,那么不需要更新该 number 的情况
|
||||
map.set(res[i],i);
|
||||
}
|
||||
}
|
||||
// 此时map里保存的每一个元素数值 对应的 小于这个数值的个数
|
||||
for(let i = 0; i < nums.length; i++){
|
||||
res[i] = map.get(nums[i]);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user