python--coding

1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

class Solution:
    def twoSum(self, nums, target):
        temp = {}
        try:
            for index,i in enumerate(nums):
                temp[index] = i
            print(temp)
            for key ,value in temp.items():
                x = target - value
                if x in nums and nums.index(x)!=key:
                    return key, nums.index(x)
        except:
            print('No two sum solution')
View Code

2.

posted on 2019-03-01 16:42  海豚竹一  阅读(123)  评论(0编辑  收藏  举报

导航