Skip to content

Commit a80797b

Browse files
Update 001_Two_Sum.py
1 parent 292f2fe commit a80797b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

001_Two_Sum.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
class Solution:
22
def twoSum(self, nums: List[int], target: int) -> List[int]:
3-
result = []
4-
for i in range(len(nums)-1):
5-
j=i+1
6-
while j < len(nums):
7-
if nums[i]+nums[j]==target:
8-
return [i, j]
9-
j += 1
10-
i += 1
11-
return result
3+
# nums_index = list(enumerate(nums))
4+
# nums_index.sort()
5+
# print(nums)
6+
for i in range(len(nums)):
7+
complement = target-nums[i]
8+
if complement in nums and nums.index(complement) != i:
9+
return [i,nums.index(complement)]
10+
# for i in range(len(nums)-1):
11+
# j=i+1
12+
# while j < len(nums):
13+
# if nums[i]+nums[j]==target:
14+
# return [i, j]
15+
# j += 1
16+
# i += 1
17+
# return result

0 commit comments

Comments
 (0)