leetcode python3 简单题1.Two Sum
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第一题
(1)题目
英文:
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.
中文:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-parentheses
(2)解法
① 使用enumerate(单循环)(耗时:1636ms,内存:14.5M)
class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:for indx, num in enumerate(nums):if target - num in nums:if indx == nums.index(target - num):continuereturn [indx, nums.index(target - num)]
注意:
1.如果nums=[3,4,5],target=6,那么第一个6-3=3是它本身,而不是两个不同数,所以要排除掉这种情况,否则无法AC,当然这个还有个大前提是,nums没有重复的元素哦。
2.list类型的.index只会返回第一个index,要想返回所有的index,使用:
[i for i in range(len(list)) if list[i] == 某个数]
3.twoSum(self, nums: List[int], target: int) -> List[int]:
这里只是对输入变量和函数输出值类型的说明,并不会提示报错。
② 使用enumerate(双循环)(耗时:5384ms,内存:14.6M)
class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:for indx1, num1 in enumerate(nums):for indx2 in range(indx1+1, len(nums)):if num1 + nums[indx2] == target:return [indx1, indx2]
注意:
1.这种结构就显然过滤掉了①中注意1.中的情况了,相当于排列组合
③ 使用hash map(字典dict)(耗时: 64ms,内存:15M)
class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:hashmap = {}for indx, num in enumerate(nums):if target - num in hashmap:return [indx, hashmap[target - num]]hashmap[num] = indx
注意:
1.这种结构就显然也能过滤掉①中注意1.中的情况
本人现在的研究方向是:
图像的语义分割,如果有志同道合的朋友,可以组队学习
haiyangpengai@gmail.com qq:1355365561
leetcode python3 简单题1.Two Sum相关推荐
- leetcode python3 简单题112. Path Sum
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十二题 (1)题目 英文: Given a binary tree and ...
- leetcode python3 简单题167. Two Sum II - Input array is sorted
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...
- leetcode python3 简单题53. Maximum Subarray
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题204. Count Primes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零四题 (1)题目 英文: Count the number of prime ...
- leetcode python3 简单题202. Happy Number
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零二题 (1)题目 英文: Write an algorithm to det ...
- leetcode python3 简单题171. Excel Sheet Column Number
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十一题 (1)题目 英文: Given a column title as ...
- leetcode python3 简单题118. Pascal's Triangle
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...
- leetcode python3 简单题70. Climbing Stairs
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...
最新文章
- 公司运作 - 利润率、周转率
- numpy向量加一个常数=向量中的每个值加上这个常数,最后返回一个同维的向量
- Python实现RGB和Lab颜色空间互转
- 数字语音信号处理学习笔记——语音信号的同态处理(4)
- Android SimpleAdapter的参数
- 利刃 MVVMLight 3:双向数据绑定
- android logcat 根据包名过滤,adb logcat通过包名过滤(dos命令find后跟变量)
- Codeforces Round #350 (Div. 2) B. Game of Robots 水题
- ListView自适应实现表格
- 银行招聘考试题库计算机,2019银行招聘计算机试题(一)答案
- 计算机发展史较为重大的事件,图说:15件计算机发展史中的重大事件(7)
- his提供哪些服务_论文查重检测系统提供哪些服务
- 山东金税盘如何跨月作废增值税普通发票
- 关于网络性能的一些指标
- 第七周作业--任务分解
- C语言——指针详细讲解
- 【洛谷】P1456 Monkey King
- QQ宠物智能辅助开发
- 【Pandas】解决在pandas中的两个正数相乘结果为负值
- 中国移动MAS机的特点
热门文章
- 工厂模式 java_JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- 安卓10侧边返回_Android 之路 (10) - 集成滑动返回(SwipeBackLayout)
- echart高级使用_Vue:在Vue中使用echarts
- 手rm-linux联网后自动dhcp,Linux操作系统下DHCP基础配置
- 轻量化网络:SqueezeNet
- Typora、github中的markdown不同的行间距
- #CSP 201912-1 报数(C语言)(100分)
- WinXP——如何重装XP系统
- Fisher准则一维聚类
- vijos-1447 开关灯泡-大整数开方算法