https://leetcode-cn.com/problems/two-sum/
题目描述
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
题解
hash表法
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
record = dict()
for i, num in enumerate(nums):
if target-num in record:
return [record[target-num], i]
else:
record[num] = i
排序双指针法
相关题目
3-sum 4-sum问题