Skip to content

Commit 292f2fe

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

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

001_Two_Sum.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
class Solution(object):
2-
3-
def twoSum(self, nums, target):
4-
# two point
5-
nums_index = [(v, index) for index, v in enumerate(nums)]
6-
nums_index.sort()
7-
begin, end = 0, len(nums) - 1
8-
while begin < end:
9-
curr = nums_index[begin][0] + nums_index[end][0]
10-
if curr == target:
11-
return [nums_index[begin][1], nums_index[end][1]]
12-
elif curr < target:
13-
begin += 1
14-
else:
15-
end -= 1
1+
class Solution:
2+
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

0 commit comments

Comments
 (0)