/
001-Two-Sum.py
44 lines (36 loc) · 1.07 KB
/
001-Two-Sum.py
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
33
34
35
36
37
38
39
40
41
42
43
44
from typing import List
import json
import sys
import io
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
temp = {}
for index,num in enumerate(nums):
if num in temp:
return [temp[num], index]
else:
temp[target - num] = index
def stringToIntegerList(input):
return json.loads(input)
def integerListToString(nums, len_of_list=None):
if not len_of_list:
len_of_list = len(nums)
return json.dumps(nums[:len_of_list])
def main():
def readlines():
for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
yield line.strip('\n')
lines = readlines()
while True:
try:
line = next(lines)
nums = stringToIntegerList(line)
line = next(lines)
target = int(line)
ret = Solution().twoSum(nums, target)
out = integerListToString(ret)
print(out)
except StopIteration:
break
if __name__ == '__main__':
main()