File tree Expand file tree Collapse file tree 2 files changed +38
-10
lines changed Expand file tree Collapse file tree 2 files changed +38
-10
lines changed Original file line number Diff line number Diff line change 2
2
"cells" : [
3
3
{
4
4
"cell_type" : " code" ,
5
- "execution_count" : null ,
5
+ "execution_count" : 1 ,
6
6
"id" : " imports" ,
7
7
"metadata" : {},
8
8
"outputs" : [],
13
13
},
14
14
{
15
15
"cell_type" : " code" ,
16
- "execution_count" : null ,
16
+ "execution_count" : 2 ,
17
17
"id" : " setup" ,
18
18
"metadata" : {},
19
19
"outputs" : [],
26
26
},
27
27
{
28
28
"cell_type" : " code" ,
29
- "execution_count" : null ,
29
+ "execution_count" : 3 ,
30
30
"id" : " run" ,
31
31
"metadata" : {},
32
- "outputs" : [],
32
+ "outputs" : [
33
+ {
34
+ "data" : {
35
+ "text/plain" : [
36
+ " [0, 1]"
37
+ ]
38
+ },
39
+ "execution_count" : 3 ,
40
+ "metadata" : {},
41
+ "output_type" : " execute_result"
42
+ }
43
+ ],
33
44
"source" : [
34
45
" result = run_two_sum(Solution, nums, target)\n " ,
35
46
" result"
36
47
]
37
48
},
38
49
{
39
50
"cell_type" : " code" ,
40
- "execution_count" : null ,
51
+ "execution_count" : 4 ,
41
52
"id" : " assert" ,
42
53
"metadata" : {},
43
- "outputs" : [],
54
+ "outputs" : [
55
+ {
56
+ "data" : {
57
+ "text/plain" : [
58
+ " True"
59
+ ]
60
+ },
61
+ "execution_count" : 4 ,
62
+ "metadata" : {},
63
+ "output_type" : " execute_result"
64
+ }
65
+ ],
44
66
"source" : [
45
67
" assert_two_sum(result, expected)"
46
68
]
60
82
"file_extension" : " .py" ,
61
83
"mimetype" : " text/x-python" ,
62
84
"name" : " python" ,
63
- "nbconvert_exporter" : " python3" ,
85
+ "nbconvert_exporter" : " python" ,
86
+ "pygments_lexer" : " ipython3" ,
64
87
"version" : " 3.13.7"
65
88
}
66
89
},
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
3
- # Time: O(? )
4
- # Space: O(? )
3
+ # Time: O(n )
4
+ # Space: O(n )
5
5
def two_sum (self , nums : list [int ], target : int ) -> list [int ]:
6
- # TODO: Implement two_sum
6
+ seen : dict [int , int ] = {}
7
+ for i , num in enumerate (nums ):
8
+ complement = target - num
9
+ if complement in seen :
10
+ return [seen [complement ], i ]
11
+ seen [num ] = i
7
12
return []
You can’t perform that action at this time.
0 commit comments