[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博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
随机推荐
- F - Fibonacci again and again
任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F(n-2)(n>=3); 所以,1, ...
- 洛谷P1162 填涂颜色【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1162 题意: 有一个0和1组成的矩阵,一些1组成一个闭合圈,圈住一些0,现在要把被圈住的这些0变成2输出. ...
- 手动安装vue-devtools
第一步:找到vue-devtools的github项目,并将其clone到本地. vue-devtools git clone https://github.com/vuejs/vue-devtool ...
- 通过Rabbitmq从ipone手机传输imu和相机数据到电脑端
https://github.com/tomas789/iOSmsg_client https://github.com/tomas789/iOSmsg 通过xcode工具把iosmsg打包发布到ip ...
- python实现支持目录FTP上传下载文件的方法
#!/usr/bin/env python # -*- coding: utf-8 -*- import ftplib import os import sys class FTPSync(objec ...
- LightOJ 1224 - DNA Prefix - [字典树上DFS]
题目链接:https://cn.vjudge.net/problem/LightOJ-1224 Given a set of $n$ DNA samples, where each sample is ...
- BZOJ 1003 - 物流运输 - [最短路+dp]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...
- centos7.2 nfs安装配置
nfs服务端 ip:192.168.1.16 1.yum -y install rpcbind nfs-utils 2.创建文件/etc/exports,内容如下 /mnt/ 192.168.1.0/ ...
- 图->定义
文字描述 顶点 指图中的数据元素 弧尾 若<v,w>属于两个顶点之间的关系的集合,表示从v到w的一条弧,则v为弧尾或初始点 弧头 若<v,w>属于两个顶点之间的关系的集合,表 ...
- python 随机模块常用命令
import randomprint(random.random()) #用于生成一个0到1之间的随机浮点数print(random.uniform(1,3))# 用于生成一个指定范围内的随机浮点数p ...