Skip to content

Commit c548628

Browse files
committed
test: add more test cases
1 parent 1b6ea02 commit c548628

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

leetcode/two_sum/playground.ipynb

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"id": "imports",
77
"metadata": {},
88
"outputs": [],
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"cell_type": "code",
16-
"execution_count": null,
16+
"execution_count": 2,
1717
"id": "setup",
1818
"metadata": {},
1919
"outputs": [],
@@ -26,21 +26,43 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": null,
29+
"execution_count": 3,
3030
"id": "run",
3131
"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+
],
3344
"source": [
3445
"result = run_two_sum(Solution, nums, target)\n",
3546
"result"
3647
]
3748
},
3849
{
3950
"cell_type": "code",
40-
"execution_count": null,
51+
"execution_count": 4,
4152
"id": "assert",
4253
"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+
],
4466
"source": [
4567
"assert_two_sum(result, expected)"
4668
]
@@ -60,7 +82,8 @@
6082
"file_extension": ".py",
6183
"mimetype": "text/x-python",
6284
"name": "python",
63-
"nbconvert_exporter": "python3",
85+
"nbconvert_exporter": "python",
86+
"pygments_lexer": "ipython3",
6487
"version": "3.13.7"
6588
}
6689
},

leetcode/two_sum/solution.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
class Solution:
22

3-
# Time: O(?)
4-
# Space: O(?)
3+
# Time: O(n)
4+
# Space: O(n)
55
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
712
return []

0 commit comments

Comments
 (0)