问题描述

给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值。

您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次。

方法 1: 蛮力

蛮力方法很简单。循环遍历每个元素 xx 并查找是否有另一个值等于目标 xtarget−x。

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(0,len(nums)):
for j in range (i+1,len(nums)):
if nums[i] + nums[j] == target:
return [i,j]

方法2: 哈希表

为了提高运行时速度, 我们需要一种更有效的方法来在数组中查找变量。维护数组中每个元素的映射到其索引的最佳方法是什么?哈希表。

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hashDict = {}
for i in range(len(nums)):
x = nums[i]
if target-x in hashDict:
return (hashDict[target-x], i)
hashDict[x] = i

此方法,在速度排行打败57%的方案

参考

https://stackoverflow.com/questions/30021060/two-sum-on-leetcode

LeetCode Two Sum 解题思路(python)的更多相关文章

  1. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. [LeetCode] Maximum Gap 解题思路

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  5. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  6. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. [LeetCode] Decode Ways 解题思路

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. LeetCode: Path Sum 解题报告

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  9. [LeetCode] Interleaving String 解题思路

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

随机推荐

  1. 江西理工大学南昌校区排名赛 D: 单身狗的物理游戏

    题目描述 萌樱花是一只单身狗. 萌樱花今天决定做一道理综物理题: 如图,两固定的绝缘斜面倾角均为θ,上沿相连.两细金属棒ab(仅标出a端)和cd(仅标出c端)长度均为L,质量分别为2m和m:用两根不可 ...

  2. UVA - 136 Ugly Numbers (有关set使用的一道题)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  3. Laravel项目部署到Nginx服务器除了/目录,全飘404

    https://blog.csdn.net/yoywow/article/details/52153610 不管是Nginx还是Apache,如果不配置,都会出现404,不能路由. 我云服务器安装的是 ...

  4. eclipse+maven远程(自动)部署web项目到tomcat

    [转自] http://blog.csdn.net/dhmpgt/article/details/11197995 eclipse集成maven后可以用maven命令把web项目自动部署到tomcat ...

  5. 子查询及pymysql

    子查询 子查询指的是当一个查询语句被作为另一个查询语句的条件时,该查询语句就称之为子查询(内层查询) 可以将一个大问题 拆分几个小的问题 然后一步一步来查询 需求:财务不有哪些人 ),sex ),jo ...

  6. google风格

    复制一下代码即可: <?xml version="1.0" encoding="UTF-8" standalone="no"?> ...

  7. rsync 参数配置说明[转]

    rsync 特性 可以镜像保存整个目录树和文件系统. 可以很容易做到保持原来文件的权限.时间.软硬链接等等. 无须特殊权限即可安装. 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修 ...

  8. Beam概念学习系列之SDKs

    不多说,直接上干货! https://beam.apache.org/get-started/beam-overview/ Beam SDK 提供了一个统一的编程模型,来处理任意规模的数据集,其中包括 ...

  9. 016-hibernateutils模板

    package ${enclosing_package}; import org.hibernate.HibernateException; import org.hibernate.Session; ...

  10. ActivityGroup和TabActiviy的差异性?

    TabActivity功能比较专一,就是做主界面Activity切换用的,所以定制性方面也就限制了许多,而且修改麻烦,不便于维护.ActivityGroup也是用来管理多个Activity的,但是功能 ...