[LeetCode]题解(python):001-Two-Sum
题目来源:
https://leetcode.com/problems/two-sum/
题意分析:
这道题目是输入一个数组和target,要在一个数组中找到两个数字,其和为target,从小到大输出数组中两个数字的位置。题目中假设有且仅有一个答案。
题目思路:
如果直接暴力解决,时间复杂度为(O(n^2)),很明显这种方法会TLE。
那么如果给定的数组是有序的会不会降低难度呢?如果是有序的数组,那么我们可以用“夹逼定理”来处理。简单来说就是首尾相加,如果比target大,则将尾数左移,如果小了首尾右移,直到两个数相加刚好等于target,那么我们可以先将数组排序,然后用“夹逼定理”,这种方法的时间复杂度为(O(nlogn)。这种方法要注意的是排序的时候要记录数组原来的位置,然后再排序。
接下来我来介绍最后一种方法。在python里面有一个dictionary的和C++ 的map功能一样。首先,我们建立一个字典,d = {},字典的key是数组的值num,value是相应的位置, 然后只要满足 num 和 target - num都在字典里面则找到答案。这种方法的时间复杂度是(O(n))。
代码(python):
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}# d is a dictionary to map the value of nums and the index in nums
size = 0
while size < len(nums):
if not nums[size] in d:
d[nums[size]] = size + 1 #if nums[size] doesn't exist in d ,create it
if target - nums[size] in d: #if nums[size] and target - nums[size] are both in d
if d[target-nums[size]] < size + 1: # one situation should be minded nums[size] == target - nums[size]
ans = [d[target - nums[size]] , size + 1]# for example [0,1,2] 0 and [0,1,2,0],0
return ans
size = size + 1
PS:注意情况,注意特殊情况。比如target刚好是数组中某个数的2倍,且这个数只有一个或者二个的时候,如[3],6和[3,2,3],6。
转载请说明出处:http://www.cnblogs.com/chruny/
[LeetCode]题解(python):001-Two-Sum的更多相关文章
- LeetCode题解之 Continuous Subarray Sum
1.题目描述 2.循环计算即可 3.代码 bool checkSubarraySum(vector<int>& nums, int k) { ){ return false ; } ...
- LeetCode 题解之Minimum Index Sum of Two Lists
1.题目描述 2.问题分析 直接是用hash table 解决问题 3.代码 vector<string> findRestaurant(vector<string>& ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- LeetCode题解——Two Sum
题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...
- LeetCode专题-Python实现之第1题:Two Sum
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用
001.Two Sum[E] Two SumE 题目 思路 1双重循环 2 排序 3 Hashmap 1.题目 Given an array of integers, return indices o ...
- [LeetCode 题解] Combination Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a se ...
- [LeetCode 题解]: Two Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- LeetCode 【1】 Two Sum --001
5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...
随机推荐
- Android中SharedPreferences函数具体解释
Android平台提供了一个SharedPreferences类,它是一个轻量级应用程序内部轻量级的存储方案,特别适合用于保存软件配置參数,比方boolean,int,float,long,Strin ...
- Asp.Net写文本日志
底层代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...
- GridView.GridLines 属性
GridLines.None 不显示网格线. GridLines.Horizontal 仅显示水平网格线. GridLines.Vertical 仅显示垂直网格线. GridLines.Both 同时 ...
- Truncate Delete 用法
Truncate /Delete Table 1.含义上都是删除表全部记录 2.Truncate 是属于数据定义语言,系统不会写每一笔记录操作事务日志,无法恢复记录数据的操作 Truncate Ta ...
- oracle 使用 decode函数 或 case when 实现行转列
----创建测试表 create table student_score( name varchar2(20), subject varchar2(20), score number(4,1) ); ...
- Hibernate工作流程
Hibernate创建步骤 (五大核心接口:Configuration/SessionFactory/Session/Transaction/Query) 1.新建工程,导入需要的jar包. 2.利用 ...
- <正向/反向>最大匹配算法(Java)
算法描述(正向): 给定最大词长n,待分词文本str,指针f=0,词典dic文档 1 取子串sub=str(f,f+n) 2 如果(遍历dic,有匹配sub) f++; 3 否则 n--; 4 注意: ...
- LaTeX空格
由于LaTeX 采用的是源文件编译方式, 默认LaTeX会忽略多余的空格, 如果需要产生一个空格,可以使用 命令 \, 注意代表的是空间键. 例如: Jones, et al.\ (1993), ...
- 使用 http://httpbin.org/ 验证代理地址
发现一个很方便的工具,在Linux 下使用 curl http://httpbin.org/ 可以返回当前使用的一些网络信息
- DDR、DDR2、DDR3产品区别
DDR采用一个周期来回传递一次数据,因此传输在同时间加倍,因此就像工作在两倍的工作频率一样.为了直观,以等效的方式命名,因此命名为DDR 200 266 333 400. DDR2尽管工作频率没有变化 ...