[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博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
随机推荐
- mysql随机查询若干条数据的方法
- win10 远程出现身份验证错误 要求的函数不受支持
win10的一个更新的bug 解决方案 http://note.youdao.com/noteshare?id=68aa9de9fbf46c50a097b3ccf7994580&sub=5AF ...
- spark on yarn 无法提交任务问题
java.lang.NoClassDefFoundError: com/sun/jersey/api/client/config/ClientConfig spark任务提交出错. 原因: spark ...
- Kernel parameter requirements ( Linux DB2)
Kernel parameter requirements ( Linux DB2) https://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/ ...
- 鸡头兔头共20,脚56,鸡兔各有多少?算法实 php现版
//$x 鸡头 //$y 兔头 for ($x = 0; $x <= 20; $x++) { for ($y = 0; $y <= 20; $y++) { if (($x + $y == ...
- HDU 5985/nowcoder 207D - Lucky Coins - [概率题]
题目链接:https://www.nowcoder.com/acm/contest/207/D 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5985 ...
- [No0000145]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈2/4
前言 虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...
- Gym - 101375H MaratonIME gets candies 交互题
交互题介绍:https://loj.ac/problem/6 题意:输出Q X ,读入><= 来猜数,小于50步猜出就算过样例 题解:根本不需要每次输出要打cout.flush()... ...
- Python:time模块、calendar模块
time模块 import time 获取时间戳 >>>time.time() #1532418950.7246091 获取时间元组 >>> time.localt ...
- jdbc实现分页,需要前端传当前页码
1.封装一个公共实体类用于返回:实体数据,当前页,总页数,总条数,每页多少条 public class PageInfo<T> { //一页显示的记录数 private int numPe ...