[LeetCode]题1:two sum
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 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.
这道题穷举法时间复杂度o(n2)太高了,通过转换为减法利用字典查找数字可以更快的完成。字典初始化和查找可合并成一个循环。另一种想法是对给定数组进行排序,首尾不断相加,直到找到指定的和。
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict={}
for i , num in enumerate(nums):
if(target-num)in dict:
return(dict[target-num],i)
else:
dict[num] = i
[LeetCode]题1:two sum的更多相关文章
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- leetcode 练习1 two sum
leetcode 练习1 two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- 简单的leetcode题
简单的leetcode题 环绕字符串中唯一的子字符串 把字符串 s 看作是\("abcdefghijklmnopqrstuvwxyz"\)的无限环绕字符串,所以 s 看起来是这样的 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- Kotlin实现LeetCode算法题之Two Sum
LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...
随机推荐
- Spring Boot 配置_yaml语法介绍 day02
一.Spring Boot 的全局配置文件(application.properties.application.yml) 1.这两种配置文件是SpringBoot 支持的对默认配置修改的格式.命名和 ...
- webpack的externals的使用
externals 官网文档解释的很清楚,就是webpack可以不处理应用的某些依赖库,使用externals配置后,依旧可以在代码中通过CMD.AMD或者window/global全局的方式访问. ...
- 4、 LwIP协议栈规范翻译——流程模型
4.流程模型 协议实现的流程模型描述了系统被划分为不同的流程的方式.用于实现通信协议的一个流程模型是让每个协议作为一个独立的进程运行.有了这个模型,严格的协议分层被强制执行,并且协议之间的通信点必须严 ...
- jquery实现一个标签图标hover到上面的时候显示tooltip
设计图: 解决思路:1.在thumbnailbox.js这个插件中加入tags弹出框显示的内容,一开始让这些内容display:none; 然后再用css画出来一个三角形 实现方法: 知识点:Jque ...
- docker容器的操作
容器操作 1.查看本地容器 docker ps -a # 查看所有容器 docker ps # 查看所有正在运行的容器 2.容器的创建与启动 方法一:两步完成(强烈推荐方法)(1)创建 docker ...
- vue的二维码生成器
vue项目开发的时候经常会遇到需要将一个网址链接转化为二维码的需求,方便用户扫描观看,以下就是我在寻找vue二维码生成器的时候踩过的坑. 不能用的二维码生成器 qrcode node-qr ...
- HDU 2586 How far away(LCA+邻接表)
How far away &题解: 和上篇是一样的题,这用的是lca方法做的, 不知道为什么,把数组开到80000 就a了 >_< 哈 我现在知道为什么了,因为我的rmq数组没有乘 ...
- puppeteer(二)操作实例——新Web自动化工具更轻巧更简单
一.入门实例 了解puppeteer见上一篇文章: https://www.cnblogs.com/baihuitestsoftware/p/9957343.html 1)本例主要是启动浏览器 con ...
- debug调试命令
- java.lang.ClassCastException: com.sun.proxy.$Proxy* cannot be cast to***
Spring AOP 有两种代理方法, 一种是常规JDK,一种是CGLIB. 当代理对象实现了至少一个接口时,默认使用JDK动态创建代理对象: 当代理对象没有实现任何接口时,就会使用CGLIB方法. ...