第一题:两数之和(easy)
环境:python3,力扣官网
题目:
解法一:
class Solution: def twoSum(self, nums: List[int], target: int): #两数之和,已经知道目标值,将target减去其中一数 for i in nums: j =target-i start = nums.index(i) next = start +1 temp =nums[next:] if j in temp: return nums.index(i),next + temp.index(j)
解法二:
class Solution: def twoSum(self, nums: List[int], target: int): dict={} for i in range(len(nums)): if target-nums[i] not in dict: dict[nums[i]] =i else: return [dict[target-nums[i]],i]