首页 > 编程语言 > 详细

python--coding

时间:2019-03-01 17:01:26      阅读:161      评论:0      收藏:0      [点我收藏+]

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

 

python--coding

原文:https://www.cnblogs.com/dolphin-bamboo/p/10457284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!