diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index d7de450b..b34ba3da 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -138,10 +138,28 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ### Python: -> (版本一)使用字典 +> 暴力法 ```python3 class Solution: + def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: + res = [0 for _ in range(len(nums))] + for i in range(len(nums)): + cnt = 0 + for j in range(len(nums)): + if j == i: + continue + if nums[i] > nums[j]: + cnt += 1 + res[i] = cnt + return res +``` + +> 排序+hash + +```python3 +class Solution: + # 方法一:使用字典 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = nums[:] hash_dict = dict() @@ -152,12 +170,8 @@ class Solution: for i, num in enumerate(nums): res[i] = hash_dict[num] return res -``` -> (版本二)使用数组 - -```python3 -class Solution: + # 方法二:使用数组 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: # 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销 sort_nums = sorted(nums)