Skip to content

Commit cbeff12

Browse files
committed
Update twoSum.py
Accepted!
1 parent 83dbe2a commit cbeff12

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/twoSum/twoSum.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
class Solution:
22
# @return a tuple, (index1, index2)
33
def twoSum(self, num, target):
4-
letarget = len(num) -1;
4+
m = {}
5+
firstIndex = 1;
6+
secondeIndex = 1;
57

6-
while num[letarget] > target:
7-
letarget = letarget - 1;
8-
9-
for i in range(0,letarget):
10-
firstnum = num[i]
11-
diff = target - firstnum;
12-
13-
secondnumlast = letarget;
14-
while num[secondnumlast] > diff:
15-
secondnumlast = secondnumlast-1;
16-
for j in range(i+1, secondnumlast+1):
17-
if num[i] + num[j] == target:
18-
return (i+1,j+1);
8+
for i in range(0,len(num)):
9+
if(m.get(num[i]) == None):
10+
m[target - num[i]] = i;
11+
else:
12+
firstIndex = m.get(num[i]) + 1
13+
secondIndex = i + 1
14+
return (firstIndex,secondIndex);

0 commit comments

Comments
 (0)