问题描述

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

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

方法 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. jstl 遍历数据

    1   导入 jstl  的  jar 包 2. 页面中添加 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/ ...

  2. UML类图关系(继承、泛化、实现、依赖、关联、聚合、组合)

    继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功能的能力,继承是类与类或者 ...

  3. 合唱团---DP

    https://www.nowcoder.com/practice/661c49118ca241909add3a11c96408c8?tpId=85&tqId=29830&tPage= ...

  4. Intellij IDEA 添加项目依赖

    https://blog.csdn.net/TaooLee/article/details/51305443 idea导入一个maven项目 https://blog.csdn.net/qq_3837 ...

  5. UML-2-迭代、进化和敏捷

    1.UP UP:Unified Process,统一过程.RUP:Rational Unified Process,Rational 公司制定的UP,是对UP的精细化. UP的过程: 初始 不是需求, ...

  6. nginx 反向代理导致的session丢失的问题

    [原文链接] https://blog.csdn.net/xiaweiyidengzhewo/article/details/80921750 注意这篇文章解释的是“丢失”而不是“一致性”

  7. ISO端form表单获取焦点时网页自动放大问题

    iOS端网页form表单输入信息时,网页自动放大,这是因为meta标签 刚开始的时候meta标签是 <meta name="viewport" content="w ...

  8. Python+Selenium之通过batch跑脚本

    例如在执行路径C:\Portal_Scripts\Scripts下的脚本CreateIndicativeBOP.py,可以在notepad里面编写如下: @echo off cd  C:\Portal ...

  9. Ubuntu系统里下载安装配置redis-2.2.13.tar.gz

    不多说,直接上干货! Redis是一个NoSQL数据库,在数据需要频繁更新,并且数据的访问热点范围比较广的应用场景下,Redis的效率很不错. 下面介绍Redis的安装过程,如下面的步骤所示. 第一步 ...

  10. python中时间对象生成及时间格式的转换

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 import time timeArray = time.strpt ...