mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
@ -149,7 +149,35 @@ class Solution:
|
||||
res[i] = hash[num]
|
||||
return res
|
||||
```
|
||||
|
||||
Go:
|
||||
```go
|
||||
func smallerNumbersThanCurrent(nums []int) []int {
|
||||
// map,key[数组中出现的数] value[比这个数小的个数]
|
||||
m := make(map[int]int)
|
||||
// 拷贝一份原始数组
|
||||
rawNums := make([]int,len(nums))
|
||||
copy(rawNums,nums)
|
||||
// 将数组排序
|
||||
sort.Ints(nums)
|
||||
// 循环遍历排序后的数组,值为map的key,索引为value
|
||||
for i,v := range nums {
|
||||
_,contains := m[v]
|
||||
if !contains {
|
||||
m[v] = i
|
||||
}
|
||||
|
||||
}
|
||||
// 返回值结果
|
||||
result := make([]int,len(nums))
|
||||
// 根据原始数组的位置,存放对应的比它小的数
|
||||
for i,v := range rawNums {
|
||||
result[i] = m[v]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
```javascript
|
||||
|
Reference in New Issue
Block a user