We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 83dbe2a commit cbeff12Copy full SHA for cbeff12
src/twoSum/twoSum.py
@@ -1,18 +1,14 @@
1
class Solution:
2
# @return a tuple, (index1, index2)
3
def twoSum(self, num, target):
4
- letarget = len(num) -1;
+ m = {}
5
+ firstIndex = 1;
6
+ secondeIndex = 1;
7
- while num[letarget] > target:
- 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);
+ for i in range(0,len(num)):
+ if(m.get(num[i]) == None):
+ m[target - num[i]] = i;
+ else:
+ firstIndex = m.get(num[i]) + 1
+ secondIndex = i + 1
+ return (firstIndex,secondIndex);
0 commit comments