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

[pull] master from youngyangyang04:master #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 8 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Dec 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Update 1365,添加python的暴力法,并将哈希法的两种方法合并写在一起
  • Loading branch information
yyccPhil authored Dec 4, 2024
commit 66caa02935bf372ac1df3e812e18aa7059fc1561
26 changes: 20 additions & 6 deletions problems/1365.有多少小于当前数字的数字.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,28 @@ public int[] smallerNumbersThanCurrent(int[] nums) {

### Python:

> (版本一)使用字典
> 暴力法

```python3
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]:
res = nums[:]
hash_dict = dict()
Expand All @@ -152,12 +170,8 @@ class Solution:
for i, num in enumerate(nums):
res[i] = hash_dict[num]
return res
```

> (版本二)使用数组

```python3
class Solution:
# 方法二:使用数组
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
# 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销
sort_nums = sorted(nums)
Expand Down

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