Skip to content

Commit 66caa02

Browse files
authored
feat: Update 1365,添加python的暴力法,并将哈希法的两种方法合并写在一起
1 parent e103494 commit 66caa02

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

problems/1365.有多少小于当前数字的数字.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,28 @@ public int[] smallerNumbersThanCurrent(int[] nums) {
138138

139139
### Python:
140140

141-
> (版本一)使用字典
141+
> 暴力法
142142

143143
```python3
144144
class 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)

0 commit comments

Comments
 (0)