Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
This is the first problem in LeetCode. The difficulty is Medium. It is not hard to solve this problem with brute force. However, an improvement of complexity can be implemented by hash table.
1 class Solution(object):
2 def twoSum(self, nums, target):
3 """
4 :type nums: List[int]
5 :type target: int
6 :rtype: List[int]
7 """
8 buff_dict = {}
9 for i, num in enumerate(nums):
10 if num in buff_dict.keys():
11 return [buff_dict[num], i + 1]
12 else:
13 buff_dict[target - num] = i + 1
The basic idea is to record the index of the “partner” of the encounter number into a python dictionary. Because python dict
use hash table, the complexity of search in dict
is \(O(1) \). So the overall complexity (consider the for
loop) is \(O(n) \) instead of \(O(n^2) \).