From bb1dd8d8c33674cacf08ecd77a50ebf072210b8e Mon Sep 17 00:00:00 2001 From: Hugh <34978586+nanqic@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:09:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=201365.=E6=9C=89=E5=A4=9A=E5=B0=91?= =?UTF-8?q?=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E5=AD=97=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Go语言版本 --- ...65.有多少小于当前数字的数字.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 9f282209..09d41e70 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -152,7 +152,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