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 2acbaeb

Browse files
🐱(hash): 1. 两数之和
1. 补充暴力解法 2. 复盘
1 parent f892af4 commit 2acbaeb

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

‎docs/data-structure/hash/README.md‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
[原题链接](https://leetcode-cn.com/problems/two-sum/submissions/)
44

5-
### 解法一
5+
### 解法一:暴力
6+
7+
```python
8+
class Solution:
9+
def twoSum(self, nums: List[int], target: int) -> List[int]:
10+
length = len(nums)
11+
for i in range(length):
12+
for j in range(i + 1, length):
13+
if nums[i] + nums[j] == target:
14+
return [i, j]
15+
```
16+
17+
- 时间复杂度:$O(n^2)$
18+
- 空间复杂度:$O(1)$
19+
20+
### 解法二:一遍哈希
621

722
利用哈希表,时间复杂度 O(n),空间复杂度 O(n)。
823

@@ -34,15 +49,20 @@ class Solution(object):
3449
return res_list
3550
```
3651

37-
### 解法二
38-
39-
时间复杂度 O(nlogn),空间复杂度 O(1)。
40-
41-
- 排序
42-
- 二分查找
43-
44-
52+
2020年06月02日 复盘:
4553

54+
```python
55+
class Solution:
56+
def twoSum(self, nums: List[int], target: int) -> List[int]:
57+
table = dict()
58+
for i in range(len(nums)):
59+
n = nums[i]
60+
tmp = target - n
61+
if tmp in table:
62+
return [i, table[tmp]]
63+
else:
64+
table[n] = i
65+
```
4666

4767
## 128. 最长连续序列
4868

0 commit comments

Comments
(0)

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