题目要求:
https://leetcode-cn.com/problems/two-sum/
这道题是接触leetcode的第一道题,思路记住了,但是列表的初始化没记住。
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d1 = {}
d2 = {}
for i in range(len(nums)):
d1[nums[i]] = i
d2[i] = target-nums[i]
for j in range(len(nums)):
k = d1.get(d2.get(j))
if k and j != k:
return j, k