-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberSumOrderedList.py
More file actions
32 lines (28 loc) · 1.09 KB
/
NumberSumOrderedList.py
File metadata and controls
32 lines (28 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import List
class NumberSumOrderedList:
dic = {}
def twoSum(self, numbers: List[int], target: int) -> List[int]:
dic = {}
for index in range(len(numbers)):
num = numbers[index]
dic[num] = index
complement = target - num
if complement in dic and index != dic[complement]:
return [index, dic[complement]]
else:
complementIndex = self.findIndexOf(numbers, complement, 0, len(numbers) - 1)
if complementIndex != -1 and index != complementIndex:
return [index, complementIndex]
def findIndexOf(self, numbers: List[int], num: int, start: int, end: int) -> int:
if start > end:
return -1
else:
mid = int((start + end) / 2)
value = numbers[mid]
#dict[value] = mid
if value > num:
self.findIndexOf(numbers, num, mid + 1, end)
elif value < num:
self.findIndexOf(numbers, num, start, mid - 1)
else:
return mid