LeetCode Two Sum 解题思路(python)
问题描述
给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值。
您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次。
方法 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)的更多相关文章
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 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 ...
- [LeetCode] Interleaving String 解题思路
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- [转] docker save与docker export的区别
[From]http://cnodejs.org/topic/59a2304f7aeedce818249eeb 很久没有写博客了,坟头草都长了老高了.写博客要靠惯性,一旦停下来时间长了,就很难再坚持下 ...
- Union Find - 20181102 - 20181105
Union Find: 589. Connecting Graph public class ConnectingGraph { //父节点数组 private int[] father = null ...
- mysql 5.7.20 server status 是stopped的解决办法
mysql 5.7.20 server status 是stopped的解决办法 在安装mysql 5.7.20的过程中,前几个过程都没什么问题,但是最后一个步骤就出问题了.当check一直提示con ...
- PIE SDK ISODATA分类
1.算法功能简介 ISODATA(IterativeSelf-OrganizingDataAnalysisTechniqueAlgorithm)即迭代式自组织数据分析技术, 其大致原理是首先计算数据空 ...
- 如何添加网页QQ在线组件
免费开通QQ推广服务 复制代码到你想要展示的位置 粘贴,完成.
- oracle12C--新特性
Orcle 12c 新特性-使用DBCA创建物理备库 >>点击这里<< Orcle 12c DG 新特性-Far Sync >>点击这里<< Orcle ...
- python 管理多版本之pyenv
一, [root@management ~]# pyenv install -listAvailable versions: 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3 ...
- 9 Essential Free Linux Transcoders(转码)
需要转码的理由千万种,所幸除了硬件转码之外,Linux平台还有很多开源工具可以借鉴,如该文章所示: 原文来自:9 Essential Free Linux Transcoders(http://www ...
- 线程同步(windows平台):事件
一:介绍 事件Event实际上是个内核对象,事件分两种状态:激发状态和未激发状态.分两种类型:手动处置事件和自动处置事件.手动处置事件被设置为激发状态后,会唤醒所有等待的线程,一直保持为激发状态,直到 ...
- 读《NoSQL精粹》前三章有感
现在NoSQL很流行,所以买了一本这方面的书,这本书虽然很薄 156页,但是感觉的确是大师的经验之谈,对于自己经验还是很少.无法能完全能心领神会,大师所说的,就像一个人说药苦,你没吃过.再听别人描述也 ...