diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 64f61096..f0a77f55 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -115,7 +115,7 @@ public: ## 其他语言版本 -### Java: +### Java: ```Java public int[] smallerNumbersThanCurrent(int[] nums) { @@ -138,18 +138,51 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ### Python: -```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() + hash_dict = dict() res.sort() # 从小到大排序之后,元素下标就是小于当前数字的数字 for i, num in enumerate(res): - if num not in hash.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况 - hash[num] = i + if num not in hash_dict.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况 + hash_dict[num] = i for i, num in enumerate(nums): - res[i] = hash[num] + res[i] = hash_dict[num] return res + + # 方法二:使用数组 + 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: @@ -220,7 +253,7 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` -### TypeScript: +### TypeScript: > 暴力法: @@ -241,7 +274,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] { }; ``` -> 排序+hash +> 排序+hash: ```typescript function smallerNumbersThanCurrent(nums: number[]): number[] { @@ -260,7 +293,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] { }; ``` -### rust +### Rust: ```rust use std::collections::HashMap; impl Solution {