思路及算法
最容易想到的方法是枚举数组中的每一个数 x,寻找数组中是否存在 target - x。
当我们使用遍历整个数组的方式寻找 target - x 时,需要注意到每一个位于 x 之前的元素都已经和 x 匹配过,因此不需要再进行匹配。而每一个元素不能被使用两次,所以我们只需要在 x 后面的元素中寻找 target - x。
#
# @lc app=leetcode.cn id=1 lang=python
#
# [1] 两数之和
#
# @lc code=start
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n = len(nums)
for i in range(n):
for j in range(i+1,n):
if nums[i]+nums[j] == target:
return [i,j]
return []
S = Solution()
print(S.twoSum([3,2,4],6))
# @lc code=end