1、Two Sum
"""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]. 思路:
用dictionary 存期望得到的数{期望数:期望数的index}(期望数= 目标数-当前数)
循环list 判断期望值是否在 dict 如果存在 则 返回对应的key 否则存期望数,接着找 """
def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict_num = {}
for key, value in enumerate(nums):
depand = target - value
if dict_num.__contains__(depand):
return dict_num.get(depand), key
dict_num[value] = key if __name__ == '__main__':
print(twoSum([, , , ], ))

LeetCode问题的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. ovs之组网实验

    介绍 本示例将创建两个OVS实例和两个主机,其中每个OVS上接入一个主机,OVS实例之间有链路连接,形成一个链状拓扑,如图.在OVS组网完成之后,再通过手动方式添加流表,实现网络通信,从而验证实验可行 ...

  2. 使用js获取页面参数

    方法一 function GetUrlParam (name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + ' ...

  3. JS学习笔记Day8

    一.内置函数Math 1.Math 1)Math.abs() 求绝对值 2)Math.PI 圆周率 2.求近似值: 1)Math.round() 四舍五入(负数: >0.5 进一 <=0. ...

  4. 深入jar包:从jar包中读取资源文件getResourceAsStream

    一.背景 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等). 在单独运行的时候这些简单的处理当然不会有问题.但是,如果我们把代码打成一个jar包以后,即使将资源文件一并打包,这些东西也找不 ...

  5. VMware 虚拟机运行卡慢的解决办法

    虚拟机的运行速度有一部分原因决定于你的物理机的配置,如果你的物理机配置不高,建议不要运行虚拟机,那样反而会更卡! 切记!!!在操作之前,先给虚拟机拍摄一个快照,然后所有操作都在虚拟机关机的情况下执行! ...

  6. opensuse 使用xx-net

    提示安装 [launcher][WARNING] import pynotify fail, please install python-notify if possible. gae_proxy][ ...

  7. Vue技术内幕 出去看看吧

    Vue的 Vue构造函数的出生 出生文件 ./instance/index  实例方法和属性 .global-api/index   静态方法和属性 Vue平台化包装 Web平台化 vue 初始化 m ...

  8. ionic 扫描二维码 Barcode Scanner、QR Scanner、ZBar

    1.简介 ionic 官方给我们提供了三个扫描二维码条形码插件,分别为: Barcode Scanner 样式好看,类似支付宝的扫描框.速度稍微比最后一个慢几百毫秒,主要问题是 Android 部分手 ...

  9. Rich feature hierarchies for accurate object detection and semantic segmentation(理解)

    0 - 背景 该论文是2014年CVPR的经典论文,其提出的模型称为R-CNN(Regions with Convolutional Neural Network Features),曾经是物体检测领 ...

  10. radio日志sim卡信号状态分析

    logcat -b radio日志 // 接着将slot 0主卡置为false,将slot 1设置为true 08-09 11:24:40.335 2565 3243 D RILJ : [4820]& ...