准备刷一刷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. SQL Server 索引基本概念与优化

    数据页和区 页 SQL Server 中的数据以“页”(Page)的形式保存数据,页是SQL Server 的IO单位,读/写一次至少是一页.一页为8K(8192byte). 页由三部分组成,页头,数 ...

  2. [转载]未能从程序集“System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”中加载类型“System.Web.WebPages.DisplayModeProvider”

    转载于http://bkcoding.cn/post_1210.html 使用vs2010开发asp.net mvc程序时,出现 未能从程序集“System.Web.WebPages, Version ...

  3. wpf expender 展开动画

    非原创,网上下载的,觉得还可以,记录一下以便以后查看学习 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2 ...

  4. 在MUI框架中使用video.js插件,并在暂停的时候利用Asp.net将观看时长保存到sqlserver数据库

    本次保存数据的情况有三种: 在视频播放的时候点击暂停,将本视频的进度保存到数据库 利用mui内部的控件,返回上一页操作时,进行保存 安卓手机触发返回键的时候,进行保存 示例一: 在video标签上面添 ...

  5. day 59 pymysql

    PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PYmysql安装 pip install pymys ...

  6. CentOS 7 - 最小化安装后,安装GNOME!!

    CentOS 7的最小化安装,并没有安装任何X Window,GNOME是比较好的一个X Window,我决定安装GNOME! 通过yum grouplist命令,我发现GNOME已经处于可以安装列表 ...

  7. BZOJ 1001--[BeiJing2006]狼抓兔子(最短路&对偶图)

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 29035  Solved: 7604 Descript ...

  8. express 重新加载

    1,res.location() 2. res.redirect() location()与redirect()的比较: Express的response对象,是对Node.js原生对象ServerR ...

  9. javaweb+spring 项目集成异常的处理

    在web项目开发中,一个系统应该要考虑到异常情况的处理,并且应该当异常发生时应该需要记录相应的异常日志,对于用户而言则不能直接抛出异常,需要考虑到用户的体验: 以下就介绍基于spring框架的基础上的 ...

  10. Linux命令学习与使用2

    1.Ctrl+a:跳到命令行首 Ctrl+E: 跳到命令行尾 Ctrl+L:清屏2.切换用户 su - 用户名3.更换yum镜像源 1.进入/etc/yum.repos.d 备份CentOS-Base ...