Leetcode title of the brush - two numbers || input an ordered array (python solve)

Subject description:

solution:

An ordered array of elements is used in the form dictionary indicates, whether there had been performed in a location other than the array for subtracting the target value in the array elements, as follows:

class Solution(object):
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int 
        """
        dic = {}
        for index, value in enumerate(numbers):
            dic[value] = index
        for index, value in enumerate(numbers):
            if dic.get(target - value) is not None:
                return [index+1, dic.get(target - value)+1]

Submit the results:

Published 248 original articles · won praise 0 · views 90000 +

Guess you like

Origin blog.csdn.net/lu_yunjie/article/details/104080184