码迷,mamicode.com
首页 > 编程语言 > 详细

力扣 1. 两数之和--python 每日一题

时间:2021-04-21 12:17:39      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:for   bsp   and   app   int   输出   lis   存在   list   

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

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

 

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]
 

提示:

2 <= nums.length <= 103
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案

 

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        L=[]
        for i in range(len(nums)):
            for j in range(len(nums)):
                if i != j and nums[i]+nums[j]==target:
                    L.append(i)
        return L

 

力扣 1. 两数之和--python 每日一题

标签:for   bsp   and   app   int   输出   lis   存在   list   

原文地址:https://www.cnblogs.com/lkc-test/p/14678599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!