1.Description
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
来源:力扣(LeetCode)
2.Example
3.Way
遍历。从第一个列表元素开始遍历每一个组合
4.Code
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
x = 0
y = 1
times1 = len(nums)
times2 = len(nums)
while (x+1) < times1:
while y < times2:
if nums[x] + nums[y] == target:
return [x, y]
break
else:
y = y + 1
x = x + 1
y = x + 1
5.Result
6.Analysis
遍历时间开销大,时间复杂度高O(n^2)