第一题Two Sum
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dict = {}
for (index,each_data) in enumerate(nums):
try:
dict[each_data].append(index)
except:
dict[each_data]=[index]
for i in dict.keys():
match = target - i
# print(dict[match])
try:
if match == i:
a = [dict[match][0]]
b = [dict[match][1]]
break
a = dict[match]
b = dict[i]
break
#print(dict[match], b)
except:
pass
return [*a, *b]
Runtime: 67 ms, faster than 55.92% of Python3 online submissions for Two Sum.
Memory Usage: 16.1 MB, less than 8.54% of Python3 online submissions for Two Sum.