-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_001_two_sum.py
More file actions
64 lines (48 loc) · 1.68 KB
/
lc_001_two_sum.py
File metadata and controls
64 lines (48 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
LeetCode 1. 两数之和 (Two Sum)
难度: Easy
分类: 哈希表
链接: https://leetcode.cn/problems/two-sum/
题目描述:
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出
和为目标值 target 的那两个整数,并返回它们的数组下标。
思路:
1. 暴力法: 双重循环,O(n²)
2. 哈希表法: 遍历时存储 {值: 索引},查找 complement = target - num
复杂度:
- 时间: O(n) - 只遍历一次
- 空间: O(n) - 哈希表存储
"""
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
哈希表解法
"""
hashmap = {} # 存储 {数值: 索引}
for i, num in enumerate(nums):
complement = target - num
# 如果 complement 已在哈希表中,找到答案
if complement in hashmap:
return [hashmap[complement], i]
# 否则将当前数存入哈希表
hashmap[num] = i
return [] # 题目保证有解,这行不会执行
# ===== 测试 =====
if __name__ == "__main__":
sol = Solution()
# 测试用例 1
nums1 = [2, 7, 11, 15]
target1 = 9
print(f"输入: {nums1}, target={target1}")
print(f"输出: {sol.twoSum(nums1, target1)}") # [0, 1]
# 测试用例 2
nums2 = [3, 2, 4]
target2 = 6
print(f"输入: {nums2}, target={target2}")
print(f"输出: {sol.twoSum(nums2, target2)}") # [1, 2]
# 测试用例 3
nums3 = [3, 3]
target3 = 6
print(f"输入: {nums3}, target={target3}")
print(f"输出: {sol.twoSum(nums3, target3)}") # [0, 1]