1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

经典two sum问题,开一个dict记录每个元素出现的位置,然后查一遍target - nums[i]在表里的什么位置,找到就是答案

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        hash_table = {}
        for i in range(0, len(nums), 1):
            if target - nums[i] in hash_table:
                return [hash_table[target - nums[i]], i]
            else:
                hash_table[nums[i]] = i
        
        
原文地址:https://www.cnblogs.com/whatyouthink/p/13254903.html