1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
思路:i = 0 nums[0] + nums[1]/nums[2]…
i = 1 nums[1] + nums[2]/nums[3]…
i =2 ……
以此类推
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 for i in range(len(nums)): 9 for j in range(i+1,len(nums)): 10 if target == nums[i] + nums[j]: 11 return [i,j] 12