mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Merge pull request #2850 from yyccPhil/master
feat: Updated题目1365,提供了Python的暴力解法,以及使用数组进行哈希的解法
This commit is contained in:
@ -115,7 +115,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
### Java:
|
### Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
public int[] smallerNumbersThanCurrent(int[] nums) {
|
public int[] smallerNumbersThanCurrent(int[] nums) {
|
||||||
@ -138,18 +138,51 @@ public int[] smallerNumbersThanCurrent(int[] nums) {
|
|||||||
|
|
||||||
### Python:
|
### Python:
|
||||||
|
|
||||||
```python
|
> 暴力法:
|
||||||
|
|
||||||
|
```python3
|
||||||
class Solution:
|
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]:
|
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
|
||||||
res = nums[:]
|
res = nums[:]
|
||||||
hash = dict()
|
hash_dict = dict()
|
||||||
res.sort() # 从小到大排序之后,元素下标就是小于当前数字的数字
|
res.sort() # 从小到大排序之后,元素下标就是小于当前数字的数字
|
||||||
for i, num in enumerate(res):
|
for i, num in enumerate(res):
|
||||||
if num not in hash.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况
|
if num not in hash_dict.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况
|
||||||
hash[num] = i
|
hash_dict[num] = i
|
||||||
for i, num in enumerate(nums):
|
for i, num in enumerate(nums):
|
||||||
res[i] = hash[num]
|
res[i] = hash_dict[num]
|
||||||
return res
|
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:
|
### Go:
|
||||||
@ -220,7 +253,7 @@ var smallerNumbersThanCurrent = function(nums) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 暴力法:
|
> 暴力法:
|
||||||
|
|
||||||
@ -241,7 +274,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
> 排序+hash
|
> 排序+hash:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function smallerNumbersThanCurrent(nums: number[]): number[] {
|
function smallerNumbersThanCurrent(nums: number[]): number[] {
|
||||||
@ -260,7 +293,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### rust
|
### Rust:
|
||||||
```rust
|
```rust
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
|
Reference in New Issue
Block a user