Python leetcode #1 Two Sum
来源:互联网 发布:域名赎回期 编辑:程序博客网 时间:2024/05/21 17:54
leetcode #1 Two Sum
首先要记住不能用双层for循环暴力破解,这样时间复杂度为O(n2)太高了受不鸟。所以先用一次循环把所有的数存到字典中,key为列表中的数,value为索引。然后在判断字典中的值有没有,用target-num计算出结果的索引。要记住记得判断nums = [3, 3], target = 6这种特殊情况。这样子的时间复杂度只有O(n)就快很多了。
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ d = {} num = 0 while num < len(nums): if not nums[num] in d: d[nums[num]] = num if target - nums[num] in d: if d[target - nums[num]] < num: return [d[target - nums[num]], num] num += 1
0 0
- Python leetcode #1 Two Sum
- 【Leetcode】【python】Two Sum, Two Sum II
- [LeetCode]1 Two Sum(C++,Python实现)
- leetcode【1+167 Two Sum 系列】【python】
- LeetCode算法(Python)--1、Two Sum
- Python leetcode记录(1) -Two Sum
- 【LeetCode 1】Two Sum(Python)
- leetcode two sum python 实现
- 【LeetCode with Python】 Two Sum
- 1.Two Sum Leetcode Python
- LeetCode : No1 Two Sum Python
- [leetcode]-[Two Sum]-python实现
- [Leetcode.python] 001. Two Sum
- [Leetcode] 1.Two Sum @python
- leetcode-001-two sum-python
- Two Sum Leetcode Python Java
- LeetCode: 1. Two Sum python
- [LeetCode] Two Sum Python 题解
- mysql初探
- X
- PCA(主成成分分析)和LDA(线性判别分析)详解-共性和区别
- fullpage.js
- 2016-2017学年第二学期C++第三章(2)
- Python leetcode #1 Two Sum
- 在安卓上部署服务器
- word2vector 一些问题及思考
- 例题 9-14 完美的服务 UVa 1218
- OpenStack Ocata 安装(八)安装过程常见报错
- BIEE-4、数据联邦
- 【HDU 4004 The Frog's Games】+ 二分
- 主流手机双摄介绍
- C++中的拷贝构造函数