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相关推荐

  1. leetcode python3 简单题112. Path Sum

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十二题 (1)题目 英文: Given a binary tree and ...

  2. leetcode python3 简单题167. Two Sum II - Input array is sorted

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...

  3. leetcode python3 简单题53. Maximum Subarray

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...

  4. leetcode python3 简单题225. Implement Stack using Queues

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...

  5. leetcode python3 简单题204. Count Primes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零四题 (1)题目 英文: Count the number of prime ...

  6. leetcode python3 简单题202. Happy Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零二题 (1)题目 英文: Write an algorithm to det ...

  7. leetcode python3 简单题171. Excel Sheet Column Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十一题 (1)题目 英文: Given a column title as ...

  8. leetcode python3 简单题118. Pascal's Triangle

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...

  9. leetcode python3 简单题70. Climbing Stairs

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...

最新文章

  1. 公司运作 - 利润率、周转率
  2. numpy向量加一个常数=向量中的每个值加上这个常数,最后返回一个同维的向量
  3. Python实现RGB和Lab颜色空间互转
  4. 数字语音信号处理学习笔记——语音信号的同态处理(4)
  5. Android SimpleAdapter的参数
  6. 利刃 MVVMLight 3:双向数据绑定
  7. android logcat 根据包名过滤,adb logcat通过包名过滤(dos命令find后跟变量)
  8. Codeforces Round #350 (Div. 2) B. Game of Robots 水题
  9. ListView自适应实现表格
  10. 银行招聘考试题库计算机,2019银行招聘计算机试题(一)答案
  11. 计算机发展史较为重大的事件,图说:15件计算机发展史中的重大事件(7)
  12. his提供哪些服务_论文查重检测系统提供哪些服务
  13. 山东金税盘如何跨月作废增值税普通发票
  14. 关于网络性能的一些指标
  15. 第七周作业--任务分解
  16. C语言——指针详细讲解
  17. 【洛谷】P1456 Monkey King
  18. QQ宠物智能辅助开发
  19. 【Pandas】解决在pandas中的两个正数相乘结果为负值
  20. 中国移动MAS机的特点

热门文章

  1. 工厂模式 java_JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
  2. 安卓10侧边返回_Android 之路 (10) - 集成滑动返回(SwipeBackLayout)
  3. echart高级使用_Vue:在Vue中使用echarts
  4. 手rm-linux联网后自动dhcp,Linux操作系统下DHCP基础配置
  5. 轻量化网络:SqueezeNet
  6. Typora、github中的markdown不同的行间距
  7. #CSP 201912-1 报数(C语言)(100分)
  8. WinXP——如何重装XP系统
  9. Fisher准则一维聚类
  10. vijos-1447 开关灯泡-大整数开方算法