Skip to content

Commit bb1dd8d

Browse files
authored
Update 1365.有多少小于当前数字的数字.md
Go语言版本
1 parent c6fadbd commit bb1dd8d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,35 @@ class Solution:
152152
res[i] = hash[num]
153153
return res
154154
```
155+
155156
Go:
157+
```go
158+
func smallerNumbersThanCurrent(nums []int) []int {
159+
// map,key[数组中出现的数] value[比这个数小的个数]
160+
m := make(map[int]int)
161+
// 拷贝一份原始数组
162+
rawNums := make([]int,len(nums))
163+
copy(rawNums,nums)
164+
// 将数组排序
165+
sort.Ints(nums)
166+
// 循环遍历排序后的数组,值为map的key,索引为value
167+
for i,v := range nums {
168+
_,contains := m[v]
169+
if !contains {
170+
m[v] = i
171+
}
172+
173+
}
174+
// 返回值结果
175+
result := make([]int,len(nums))
176+
// 根据原始数组的位置,存放对应的比它小的数
177+
for i,v := range rawNums {
178+
result[i] = m[v]
179+
}
180+
181+
return result
182+
}
183+
```
156184

157185
JavaScript:
158186
```javascript

0 commit comments

Comments
 (0)