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 = ...
随机推荐
- 单个html使用axios调用接口传参
单个html页面使用axios调用接口传参(没有使用v-cli搭建框架,也没有使用qs等等) 1.使用 URLSearchParams的方法 var params = new URLSearchPar ...
- Git的深入理解与GitHub托管服务的使用
[转自] http://www.cnblogs.com/cocowool/archive/2012/02/17/2356125.html 源代码管理系统(SCM)与版本控制 版本控制是一种记录若干文件 ...
- mutillidae之Insert型注入
A1:Insert型注入 1.输入内容,确定内容输出位置,确定插入字段顺序 输入test')-- -,页面报错,可知test并非最后一个字段,继续尝试test','123'),页面返回正常,确定tes ...
- postgreSQL 常用命令 二
本次测试基与PostgreSQL 10.x版本 创建用户 [postgres@rtm2 data]$ /opt/pgsql-10/bin/createuser rentaomin [postgres@ ...
- 红米note_维修_开机键
1. 2.在线人工客服(20180919) 很荣幸为您服务,有什么问题可以帮助到您的- 我的手机 后边的 开机键 貌似 不太行了 您好,您是哪款手机 就是 要按 好几次 很用力 才能 开亮手机屏幕木 ...
- zabbix web url监控
一, web监控 这个监控为通过cookie的值来监控网站是否能正常使用 这里测试环境为bbs网站 二, 配置web监控 01, 创建web监控项 02,配置步骤1 查看数据是否成功 第一查看首页时候 ...
- c# 屏蔽快捷键
前言 有时候开发会遇到这样一个需求,软件需要屏蔽用户的组合快捷键或某些按键,避免强制退出软件,防止勿操作等. 原理 1.要实现组合键,按键拦截,需要用到user32.dll中的SetWindowsHo ...
- filter get乱码 全站编码解决 包装模式
包装模式简介: package com.itheima.test; import java.io.BufferedReader; import java.io.IOException; import ...
- JavaScript基础(String)
字符串 String 1.连接字符串 数字与字符串相加,为字符串 9+"9"; 2.字符串的长度 "abc".length; 3.从字符串中获取单个字符(索引下 ...
- maven module
通过将一个maven项目拆分成多个module,会引入一定的项目复杂度,但随着后期项目代码的逐渐增多,最直观的感受是,每次build代码,不必build整个项目,可节省很多时间. 如果各个module ...