File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -138,10 +138,28 @@ public int[] smallerNumbersThanCurrent(int[] nums) {
138138
139139### Python:
140140
141- > (版本一)使用字典
141+ > 暴力法
142142
143143```python3
144144class Solution :
145+ def smallerNumbersThanCurrent(self, nums: List[ int] ) -> List[ int] :
146+ res = [ 0 for _ in range(len(nums))]
147+ for i in range(len(nums)):
148+ cnt = 0
149+ for j in range(len(nums)):
150+ if j == i:
151+ continue
152+ if nums[ i] > nums[ j] :
153+ cnt += 1
154+ res[ i] = cnt
155+ return res
156+ ```
157+
158+ > 排序+hash
159+
160+ ```python3
161+ class Solution:
162+ # 方法一:使用字典
145163 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
146164 res = nums[:]
147165 hash_dict = dict()
@@ -152,12 +170,8 @@ class Solution:
152170 for i, num in enumerate(nums):
153171 res[i] = hash_dict[num]
154172 return res
155- ```
156173
157- > (版本二)使用数组
158-
159- ```python3
160- class Solution:
174+ # 方法二:使用数组
161175 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
162176 # 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销
163177 sort_nums = sorted(nums)
You can’t perform that action at this time.
0 commit comments