1 '''
2 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数。
3 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
4 示例: 给定 nums = [2, 7, 11, 15], target = 9
5 因为 nums[0] + nums[1] = 2 + 7 = 9
6 所以返回 [0, 1]
7 '''
8
9
10 class Solution:
11
12 def twoSum(self, nums, target):
13 """
14 :type nums: List[int]
15 :type target: int
16 :rtype: List[int]
17 """
18 for i in range(0, len(nums)):
19 try:
20 j = nums.index(target - nums[i])
21 except:
22 return '', '没有符合条件的元素'
23 else:
24 return i, j
25
26
27 if __name__ == '__main__':
28 nums = [2, 7, 11, 15, 20, 12, 30, 28]
29 target = 17
30 s = Solution()
31 i, j = s.twoSum(nums, target)
32 print(i, j)