[LeetCode] 1. Two Sum_Easy
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].
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
for index, num in enumerate(nums):
if target -num in d:
return [d[target-num], index]
d[num] = index
#return [] # 可以不要, 因为题目说肯定有一个solution
[LeetCode] 1. Two Sum_Easy的更多相关文章
- [LeetCode] 112. Path Sum_Easy tag: DFS
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 1. Two Sum_Easy tag: Hash Table
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- [LeetCode] questions conclustion_Path in Tree
Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS input: root, target, return True if exi ...
- [LeetCode] questions conclustion_BFS, DFS
BFS, DFS 的题目总结. Directed graph: Directed Graph Loop detection and if not have, path to print all pat ...
- [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
随机推荐
- 基于jdk1.8的HashMap源码学习笔记
作为一种最为常用的容器,同时也是效率比较高的容器,HashMap当之无愧.所以自己这次jdk源码学习,就从HashMap开始吧,当然水平有限,有不正确的地方,欢迎指正,促进共同学习进步,就是喜欢程序员 ...
- 异常could not retrieve snapshot
前两天项目升级,项目部署到生产上之后,报错: could not retrieve snapshot 上网查的结果是: “.hbm.xml中的字段没有与数据库中一样,就是说有些字段在文件中有,但是在数 ...
- .net core中使用Type.GetType()从字符串获取类型遇到的问题
问题背景是想在 appsettings.json 中动态配置依赖注入,依赖注入代码如下: services.AddSingleton(typeof(ISmsService), Type.GetType ...
- 制作tomcat重启.bat文件
环境是在windows下,linux不知道,没有尝试过,有机会去试试哈哈 首先创建一个restart.bat文件 创建在哪里看你开心咯 然后用记事本打开 输入以下源代码: @echo off cd / ...
- AD 16 下绘图的几个技巧
1.绘制封装如果引脚过多怎么办,使用阵列粘贴功能 首先建立一个焊盘,然后选中,使用 ctrl + c 复制,注意复制确认的时候,鼠标一定要点击到焊盘中间. 选择阵列粘贴 条款就是你要复制多少个,增量就 ...
- [GRE] GRE协议介绍
写的一般,主要看下图就行了. https://blog.csdn.net/Mary19920410/article/details/72303641 前半部分介绍还不错,后半部分没看. http:// ...
- [development][c++] C++构造函数调用构造函数
构造函数调用构造函数是会问题的. 外层函数返回的内存, 与被调用的构造函数返回的内存并不是一个内存. 错误示例代码如下: msg_log(const char *name, const char* t ...
- LeetCode 590 N-ary Tree Postorder Traversal 解题报告
题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...
- ipv6的校验格式
ipv6的校验格式: ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
- java 线程(五)线程安全 Lock接口
package cn.sasa.demo3; import java.util.concurrent.ExecutionException; public class ThreadDemo { pub ...