准备刷一刷LeetCode了。

题目:

'''
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. Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
'''

中文题目:

'''
给定一个整数数组,返回两个元素的索引,使得这两个元素相加的值等于一个给定的target。 您可以假设每个输入都恰有一个解决方案。 例:
给定nums = [2,7,11,15],target = 9, 因为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]
"""
arr = {} # 定义一个空字典
length = len(nums)
for i in range(length):
if (target - nums[i]) in arr: # 如果target减去当前的元素的值存在于空字典中时,符合题意。
return [arr[target - nums[i]], i] # 返回target-nums[i]和i的索引
arr[nums[i]] = i # 否则将该元素加入字典arr # 该方法accepted了,不知道是否有更优的解法
a = Solution().twoSum([3, 2, 4], 6)
print(a)

LeetCode【第1题】Two Sum的更多相关文章

  1. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  2. leetcode第38题--Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  3. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  4. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  5. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  6. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. leetcode 入门第一题 4ms? 8ms? Two Sum

    今天开启leetcode 入门第一题 题意很简单,就是一个数组中求取两数之和等于目标数的一对儿下标 1.暴力 n^2 两个for循环遍历 用时0.1s 开外 代码就不用写了 2.二分 nlogn 我们 ...

  8. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  10. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. console的所有用法

    http://jingyan.baidu.com/article/e75aca855c6419142edac6c1.html  参考它. console.info(), console.debug() ...

  2. 【转】【译】在 Windows 10 应用程序中注册任意依赖属性的改变

    原文地址:http://visuallylocated.com/post/2015/04/01/Registering-to-any-DependencyProperty-changing-in-Wi ...

  3. Redis 慢查询

    Redis 慢查询   许多存储系统提供慢查询日志帮助开发和运维人员定位系统的慢操作.慢查询日志就是系统在命令执行前后计算每条命令的执行时间,当超过预设阈值就将这条命令的相关信息记录下来Redis提供 ...

  4. fleet中service之间的依赖关系

    最近有人在topcoder上提出使用fleet在集群上部署service时有时候会发现,当启动依赖于整个集群服务的service时,只会检查那个service所在机器的依赖关系,这样就会造成一些问题, ...

  5. Linux - 修改文件编码

    enca -L zh_CN -x UTF- file

  6. 学习总结---INNODB 事务并发

    目前在做一个OLTP的数据库系统,批量读写和随机读写并发,情况比较复杂.INNODB是我们的MYSQL引擎,他的主要特点是读操作可以不受阻塞,而修改操作会加锁.如何才能最高效的使用innodb是我们需 ...

  7. 一个初学者的指南,使用D3做数据绑定

    一个初学者的指南,使用D3做数据绑定 D3.js 是个强大的数据可视化库,可以做出惊艳的图表.比如:气泡图,线图和条形图--只需要很少行的代码 随着初学者对JavaScript的理解,可以将数组或者对 ...

  8. [ASP.NET].NET逻辑分层架构总结

    一.基础知识准备: 1.层的原则: (1)每一层以接口方式供上层调用. (2)上层只能调用下层. (3)依赖分为松散交互和严格交互两种. 2.业务逻辑分类: (1)应用逻辑. (2)领域逻辑. 3.采 ...

  9. Exp2 后门原理与实践 20164321 王君陶

    Exp2 后门原理与实践 20164321 王君陶 一.实验内容 基础问题回答: 1.例举你能想到的一个后门进入到你系统中的可能方式? 答:通过漏洞,点击陌生链接,或者浏览不良网页挂马. 2.例举你知 ...

  10. Windows下TensorFlow安装指南(图文版)

    随着深度学习概念火起来,TensorFlow也跟着成为业界流行的深度学习框架.它采用CPU+GPU的并行计算模式,使得神经网络可以有效的并行计算,从以前的三层网络到现在的深层网络,深度学习+tenso ...