Skip to content

Commit 7c49fbb

Browse files
committed
added Two Sum
1 parent 212e82e commit 7c49fbb

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

LeetCode/0001_Two_Sum.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
class Solution(object):
22
def twoSum(self, nums, target):
3-
if len(nums) <= 1:
4-
return False
5-
buff_dict = {}
6-
for i in range(len(nums)):
7-
if nums[i] in buff_dict:
8-
return [buff_dict[nums[i]], i]
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
dic = {}
9+
num_of_nums = len(nums)
10+
i = 0
11+
while i < num_of_nums:
12+
num = target - nums[i]
13+
if (num in dic):
14+
return [dic[num], i]
915
else:
10-
buff_dict[target - nums[i]] = i
16+
dic[nums[i]] = i
17+
i = i + 1
18+
19+
20+

0 commit comments

Comments
 (0)