@ [TOC] (fastening force (leetcode 1) of two numbers and python)
topic
Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/two-sum
Given an integer array nums and a target value target, you find in the array and the target value of that two integers, and return to their array subscript.
You can assume that each input corresponds to only one answer. However, you can not re-use the same array element.
Given nums = [2, 7, 11 , 15], target = 9
because nums [0] + nums [1 ] = 2 + 7 = 9
is returned [0, 1]
Code
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dic={}
for i,j in enumerate(nums):
k=target-j
if k in dic:
return [dic[k],i]
dic[j]=i