From 5b94b448646e15507c07aa5c95337b2f1bd0f620 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:29:18 -0600 Subject: [PATCH] =?UTF-8?q?feat:=20Updated=E9=A2=98=E7=9B=AE1365=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E4=BA=86=E4=BD=BF=E7=94=A8=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=93=88=E5=B8=8C=E7=9A=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...65.有多少小于当前数字的数字.md | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 64f61096..22dd3226 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -138,7 +138,9 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ### Python: -```python +> (版本一)使用字典 + +```python3 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = nums[:] @@ -152,6 +154,23 @@ class Solution: return res ``` +> (版本二)使用数组 + +```python3 +class Solution: + def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: + # 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销 + sort_nums = sorted(nums) + # 题意中 0 <= nums[i] <= 100,故range的参数设为101 + hash_lst = [0 for _ in range(101)] + # 从后向前遍历,这样hash里存放的就是相同元素最左面的数值和下标了 + for i in range(len(sort_nums)-1,-1,-1): + hash_lst[sort_nums[i]] = i + for i in range(len(nums)): + nums[i] = hash_lst[nums[i]] + return nums +``` + ### Go: ```go