LeeCode-两数之和(python)

简介: LeeCode-两数之和(python)

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 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 <= 104

-109 <= nums[i] <= 109

-109 <= target <= 109

只会存在一个有效答案


来源:力扣(LeetCode)

链接:https://leetcode.cn/problems/two-sum


class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        a = []
        for i in range(0, len(nums)):
            for j in range(i+1, len(nums)):
                if(nums[i]+nums[j] == target):
                    a.append(i)
                    a.append(j)
                    break
                else:
                    continue
        return a
目录
相关文章
|
1月前
|
存储 算法 Python
python 算法 两数之和 的多种解法
python 算法 两数之和 的多种解法
|
1月前
|
人工智能 机器人 测试技术
【python】两数之和 python实现(详细讲解)
【python】两数之和 python实现(详细讲解)
|
20天前
|
存储 算法 Java
【经典算法】LeetCode1:两数之和(Java/C/Python3实现含注释说明,Easy)
【经典算法】LeetCode1:两数之和(Java/C/Python3实现含注释说明,Easy)
14 1
|
16天前
|
存储 SQL 数据挖掘
LeetCode 第一题:两数之和 【1/1000 python】
LeetCode 第一题:两数之和 【1/1000 python】
|
11月前
|
存储 算法 索引
Python OJ题典例:两数之和
本文介绍了解决两数之和问题的思路和方法。相信阅读后将对两数之和问题有更深入的理解。
92 0
|
算法 Python
Python每日一练(20230502) 围绕区域、两数之和、二叉树展开
Python每日一练(20230502) 围绕区域、两数之和、二叉树展开
67 0
|
Python
LeeCode-合并两个有序链表(python)递归
LeeCode-合并两个有序链表(python)递归
105 0
LeeCode-合并两个有序链表(python)递归
|
算法 Java Python
LeeCode-盛最多水的容器(python)-双指针解法
LeeCode-盛最多水的容器(python)-双指针解法
109 0
LeeCode-盛最多水的容器(python)-双指针解法
|
存储 Python
LeeCode-两数相加(python)
LeeCode-两数相加(python)
86 0
LeeCode-两数相加(python)
|
前端开发 Python
Leecode加法题目3个 每日练习 Python实现
Leecode加法题目3个 每日练习 Python实现
93 0
Leecode加法题目3个 每日练习 Python实现