1. 两数之和
一、问题描述
二、算法思想
暴力算法,从头加到尾时间复杂度O(n^2)。
三、代码
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
length = len(nums)
i=0
while i < length-1:
k = i+1
while (nums[i] + nums[k] != target) :
if k < length-1:
k += 1
else:
break
if nums[i] + nums[k] == target:
a=[i,k]
return a