Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6973320

Browse files
Merge pull request youngyangyang04#2850 from yyccPhil/master
feat: Updated题目1365,提供了Python的暴力解法,以及使用数组进行哈希的解法
2 parents 2c89ed9 + 9b56f0a commit 6973320

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

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

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public:
115115
116116
## 其他语言版本
117117
118-
### Java:
118+
### Java:
119119
120120
```Java
121121
public int[] smallerNumbersThanCurrent(int[] nums) {
@@ -138,18 +138,51 @@ public int[] smallerNumbersThanCurrent(int[] nums) {
138138

139139
### Python:
140140

141-
```python
141+
> 暴力法:
142+
143+
```python3
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
142161
class Solution:
162+
# 方法一:使用字典
143163
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
144164
res = nums[:]
145-
hash = dict()
165+
hash_dict = dict()
146166
res.sort() # 从小到大排序之后,元素下标就是小于当前数字的数字
147167
for i, num in enumerate(res):
148-
if num not in hash.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况
149-
hash[num] = i
168+
if num not in hash_dict.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况
169+
hash_dict[num] = i
150170
for i, num in enumerate(nums):
151-
res[i] = hash[num]
171+
res[i] = hash_dict[num]
152172
return res
173+
174+
# 方法二:使用数组
175+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
176+
# 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销
177+
sort_nums = sorted(nums)
178+
# 题意中 0 <= nums[i] <= 100,故range的参数设为101
179+
hash_lst = [0 for _ in range(101)]
180+
# 从后向前遍历,这样hash里存放的就是相同元素最左面的数值和下标了
181+
for i in range(len(sort_nums)-1,-1,-1):
182+
hash_lst[sort_nums[i]] = i
183+
for i in range(len(nums)):
184+
nums[i] = hash_lst[nums[i]]
185+
return nums
153186
```
154187

155188
### Go:
@@ -220,7 +253,7 @@ var smallerNumbersThanCurrent = function(nums) {
220253
};
221254
```
222255

223-
### TypeScript:
256+
### TypeScript:
224257

225258
> 暴力法:
226259

@@ -241,7 +274,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] {
241274
};
242275
```
243276
244-
> 排序+hash
277+
> 排序+hash:
245278
246279
```typescript
247280
function smallerNumbersThanCurrent(nums: number[]): number[] {
@@ -260,7 +293,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] {
260293
};
261294
```
262295

263-
### rust
296+
### Rust:
264297
```rust
265298
use std::collections::HashMap;
266299
impl Solution {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /