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) {
138
138
139
139
### Python:
140
140
141
- > (版本一)使用字典
141
+ > 暴力法
142
142
143
143
```python3
144
144
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
+ # 方法一:使用字典
145
163
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
146
164
res = nums[:]
147
165
hash_dict = dict()
@@ -152,12 +170,8 @@ class Solution:
152
170
for i, num in enumerate(nums):
153
171
res[i] = hash_dict[num]
154
172
return res
155
- ```
156
173
157
- > (版本二)使用数组
158
-
159
- ```python3
160
- class Solution:
174
+ # 方法二:使用数组
161
175
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
162
176
# 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销
163
177
sort_nums = sorted(nums)
You can’t perform that action at this time.
0 commit comments