Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python学习:关于Two Sum问题的一些记录

Python学习:关于Two Sum问题的一些记录

作者头像
烤粽子
发布于 2021-07-07 11:18:48
发布于 2021-07-07 11:18:48
49200
代码可运行
举报
运行总次数:0
代码可运行

题目:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
'''
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.'''

1. 最开始的写法:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        if len(nums) <1 :
            return false
        for i in range(len(nums)):
            temp=target-nums[i]
            if temp in nums.index
            while j

大部分例子都能通过,有一个出现了如下图所示的提示:

2. 利用index改写为:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def twoSum(self, nums, target):       
        if len(nums) <1 :
            return false
        for i in range(len(nums)):
            temp=target-nums[i]
            if temp in nums:
                if i!=nums.index(temp):
                    return i,nums.index(temp)

3. 利用enumerate()来改写:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
def twoSum(self, num, target):
    d = dict()
    for index, number in enumerate(num):
        try:
            return ((d[target-number]+1, index+1))
        except:
            d[number] = index

看到有加了try的,感觉这样很好,借鉴一下。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enumerate(sequence, [start=0])
  • sequence,序列或迭代对象等
  • start 下标起始位置
  • 返回值:返回枚举对象,
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> sq=['one','two','three']
>>> for index,element in enumerate(sq):
	print(index,sq[index])


	
0 one
1 two
2 three
>>> 

4. 

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    # @return a tuple, (index1, index2)
    # 8:42
    def twoSum(self, num, target):
        map = {}
        for i in range(len(num)):
            if num[i] not in map:
                map[target - num[i]] = i + 1
            else:
                return map[num[i]], i + 1

        return -1, -1
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/05/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【LeetCode题解---1】Two Sum
“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.”
周三不加班
2019/09/04
3530
【LeetCode题解---1】Two Sum
[LeetCode题解]开篇!求和问题总结:2Sum/3Sum/4Sum/KSum
之前在美国做访学,日子比较清闲。当时对数据结构和算法几乎一窍不通,便开始在Leetcode上刷算法题,也算是为找工作做准备,经过了一年多,也积累了很多Leetcode题解文章,并在CSDN上开了题解专栏。
Rude3Knife的公众号
2019/08/06
1.7K0
【记录帖】(No.001)从零打卡刷Leetcode
小詹一直觉得自己编程能力不强,想在网上刷题,又怕不能坚持。不知道有木有和小伙伴和小詹一样想找个人一起刷题呢?欢迎和小詹一起定期刷leetcode,每周一周五更新一题,每一题都吃透,欢迎一题多解,寻找最优解!欢迎小伙伴们把自己的思路在留言区分享出来噢~
小小詹同学
2018/07/24
4040
【记录帖】(No.001)从零打卡刷Leetcode
[Leetcode][Python/Java]两数之和 Two Sum/两数之和 II - 输入有序数组 Two Sum II
https://leetcode-cn.com/problems/two-sum/solution/
蛮三刀酱
2019/03/26
8760
LeetCode 1:两数之和 Two Sum
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
爱写bug
2019/10/15
4240
LeetCode 1:两数之和   Two Sum
Q1 Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], targ
echobingo
2018/04/25
5310
LeetCode Algorithm
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
MachineLP
2019/05/26
3980
LeetCode(一)
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Rekent
2018/09/04
2860
LeetCode 两数之和 Python
参考:1. 两数之和-Python-LeetCode 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 解法一: class Solution: def twoSum(self, nums, target): """
2018/09/04
8590
LeetCode题解001:两数之和
​ hashmap[num] = i #这句不能放在if语句之前,解决list中有重复值或target-num=num的情况 不过方法四相较于方法三的运行速度没有像方法二相较于方法一的速度提升。运行速度在 70ms 多
对弈
2019/09/15
5720
LeetCode - 001
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
咸鱼学Python
2019/06/03
4280
Leetcode 001: Two Sum
  Given an array of integers, return indices of the two numbers such that they add up to a specific target.
西湖醋鱼
2020/12/30
2430
【打卡贴】(No.001)从零开始刷LeetCode
打卡刷LeetCode是受小詹的启发,自己也会在LeetCode刷题之前只是在网上做完就行了,今年在刷题的时候突然想做一下记录以后做回顾,之后每天都在有道云笔记做点记录,现在既然开了公众号索性就增加这个专栏。每天一题每一题都吃透,希望看到自己成长的点点滴滴。我会用两种语言来解决所有问题,专科的时候主修java现在本科自学python,所以两种语言都做一个尝试。
PM小王
2019/07/01
4810
【打卡贴】(No.001)从零开始刷LeetCode
LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
爱写bug
2019/07/06
3110
python 算法 两数之和 的多种解法
两数之和问题是LeetCode上的一道经典算法题,也是面试中常见的问题。题目要求在一个整数数组中找到两个数,使它们的和等于一个特定的目标值,并返回这两个数的下标。
编程小白狼
2024/12/31
1050
LeetCode 1. Two Sum分析代码
利用hashmap存储每个元素的值和所在的下标,遍历一遍,如果target-nums[i]在map里直接取出index值返回就可以了。leetcode 的第一道题。
desperate633
2018/08/22
2690
Leetcode第一题:两数之和(3种语言)
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
全栈程序员站长
2022/07/01
4250
Leetcode第一题:两数之和(3种语言)
leetCode
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
py3study
2020/01/02
2460
LeetCode:1_Two_Sum | 两个元素相加等于目标元素 | Medium
题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that y
Linux云计算网络
2018/01/11
5930
【leetcode刷题】T3-Two Sum II
今天本来打算更新3sum closest这道题,但是发现需要用到Two Sum II的思想。
木又AI帮
2019/07/17
4160
【leetcode刷题】T3-Two Sum II
相关推荐
【LeetCode题解---1】Two Sum
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验