two sum python_Python | Leetcode 之 Two Sum
说来惭愧,到现在才开始刷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, and you may not use the sameelement twice.
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
先上暴力解法:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
length = len(nums)
for i in range(length) :
for j in range(length):
if nums[i] == target - nums[j] and i != j:
return [i, j]
Submit看结果:
Runtime: 7780 ms, faster than 5.01% of Python3 online submissions for Two Sum.
Memory Usage: 13.8 MB, less than 24.07% of Python3 online submissions for Two Sum.
即使是第一道题,但这结果也太差了吧
两个for循环做了无用功,如果把list分成两部分来计算,也就是只用for循环一次呢?
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
k = 0
for i in nums:
k += 1
if target - i in nums[k:]:
return(k -1, nums[k:].index(target - i) + k)
Submit看结果:
Runtime: 848 ms,faster than32.81%ofPython3online submissions forTwo Sum.
仍旧是连一半都没超过啊,娘匹西。
官方方法 Two-Pass Hash Table
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashTable = {}
length = len(nums)
for i in range(length):
hashTable[nums[i]] = i
for i in range(length):
if target - nums[i] in hashTable and hashTable[target - nums[i]] != i:
return [i, hashTable[target - nums[i]]]
return([])
Submit看结果:
Runtime: 48 ms, faster than58.71%ofPython3online submissions forTwo Sum.
官方还提供One-Pass Hash Table,也就是每次插入一个元素,然后检查这个元素是否符合,以此类推。
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashTable = {}
for i, num in enumerate(nums):
if target - num in hashTable:
return([hashTable[target - num], i])
break
hashTable[num] = i
return([])
但这个结果submit的结果并没有表现得如官方所说的那样。
总结:结果这个问题的方法有很多,从暴力到哈希表,除了要熟知基本的元素操作,便是懂得时间复杂度和空间复杂度的一个balance
关于Python语法的细节还是要加强,不然卡住的地方太多了
哈希表的概念要再加强一些
two sum python_Python | Leetcode 之 Two Sum相关推荐
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- [LeetCode] 303. Range Sum Query - Immutable
https://leetcode.com/problems/range-sum-query-immutable/ 用一个 sum 数组,sum[i] -- nums 中选出前 i 个元素,求和所得到的 ...
- leetcode 437. Path Sum III | 437. 路径总和 III(DFS,前缀和)
题目 https://leetcode.com/problems/path-sum-iii/ 题解 DFS+前缀和问题,注意节点可能为 0 的情况(也是因此发现应该用 HashMap 记录前缀和的个数 ...
- leetcode 677. Map Sum Pairs | 677. 键值映射(Trie前缀树,BFS)
题目 https://leetcode.com/problems/map-sum-pairs/ 题解 基于前缀树实现,可以参考:leetcode 208. Implement Trie (Prefix ...
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- leetcode 303. Range Sum Query - Immutable | 303. 区域和检索 - 数组不可变(一维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-immutable/ 题解 标准的前缀和问题,简单题,不多说,直接上代码 import java.ut ...
- 【动态规划】LeetCode 377. Combination Sum IV
LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...
- 【DFS】LeetCode 39. Combination Sum
LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
最新文章
- linux 文件拷贝并替换,Linux_cmd replace 文件替换使用说明,帮助信息: 复制代码 代码如 - phpStudy...
- JQuery 常用积累(六)ZTree
- OGNL使用方法总结
- PyCairo 教程
- [Java基础]Lambda表达式的注意事项
- Servlet---注解开发
- 04_Spring中使用Quartz
- 《纲要》落地,东方通教你挖数据金矿
- java单链表选票_Hackerrank Practice
- ASP.NET中防止页面多次提交的代码实现
- 考勤管理系统需求文档
- soapui脚本链接mysql_SOAPUI中文教程---脚本和脚本库
- 人工智能6.1 -- 机器学习算法篇(一)数据清洗、回归(含实践)
- 合并排序-MergeSort
- 龙芯2F 8089D 安装 debian 8.10
- 基础篇——树莓派远程连接工具VNC不显示视频或摄像头画面解决方式
- 双语美文|治愈系英文段子,你对人生过敏吗
- mysql综训实训报告总结_实训报告总结收获.doc
- java http请求发送unicode_c++ 使用httpclient获取网页及utf8与unicode之间转码
- 第四次机考(2019) C. f1二
热门文章
- C++函数指针使用总结
- 大数据生态圈常用组件(二):概括介绍、功能特性、适用场景
- 小甲鱼 OllyDbg 教程系列 (二) :从一个简单的实例来了解PE文件
- lg空调代码大全解决_LG空调故障代码大全
- linux模式匹配运算符,linux之正则表达式
- 中科大计算机学院博士导师,中科大计算机学院招生导师
- python中打开文件open_Python中打开文件的方式(With open)
- 主流java框架理解
- 超级计算机游戏电脑,Salad邀请PC玩家参与全球最大分布式超级计算机的构建
- linux 多线程客户端服务端通信,[转载]多线程实现服务器和客户端、客户端和客户端通信;需要代码,留言...