《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用
001.Two Sum[E]
1.题目
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
2.思路
2.1双重循环
最简单的思路,两重循环,没啥技术含量,速度很慢,毕竟是O(N2)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
for(int i = 0;i < nums.size();i++)
{
for(int j = i+1;j < nums.size();j++)
if(nums[i] + nums[j] == target)
{
result.push_back(i);
result.push_back(j);
return result;
}
}
}
};
2.2 排序
其实简单的想,用一个排序都能把复杂度降到O(NlogN),通过排序,然后用两个指针从前后扫描逼近真值,因为一定会有一个值满足,然后通过值去原数组里找对应的下标(这里其实就可以考虑,如果当初就用一个数据结构存好键值对应关系就好了,其实就是hashmap,下面的做法就用到了)
(下面代码是 dugu9sword写的java代码,我就没写成c++的,主要看思路,还是比较好理解的)
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] nums_sorted=new int[nums.length];
System.arraycopy(nums,0,nums_sorted,0,nums.length);
//Quicksort.
Arrays.sort(nums_sorted);
//Find the two numbers. O(n)
int start=0;
int end=nums_sorted.length;
while(start<end){
while(nums_sorted[start]+nums_sorted[--end]>target);
if(nums_sorted[end]+nums_sorted[start]==target)
break;
while(nums_sorted[++start]+nums_sorted[end]<target);
if(nums_sorted[end]+nums_sorted[start]==target)
break;
}
//find the indices of the two numbers
int[] ret=new int[2];
int index=0;
int a=nums_sorted[start];
int b=nums_sorted[end];
for(int i=0;i<nums.length;i++)
if(nums[i]==a || nums[i]==b)
ret[index++]=i;
return ret;
}
}
2.3 Hashmap
最后一种是比较聪明的做法,用hashmap,hashmap是内部存储方式为哈希表的map结构,哈希表可以达到查找O(1),哈希表的介绍可以看这里, C++实现Hashmap的方式,这里用unordered_map关联容器,可以实现键值对应。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
unordered_map<int,int> mymap;
int res;
for(int i = 0;i < nums.size();i++)
{
res = target - nums[i];
unordered_map<int,int>::iterator it = mymap.find(res);
if(it != mymap.end())
{
return vector<int>({it->second,i});
}
mymap[nums[i]] = i;
}
}
};
《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用的更多相关文章
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- LeetCode题解 #1 Two Sum
在LeetCode做的第一到题 题目大意:给出n个数,在其中找出和为一个特定数的两个数. Input: numbers={2, 7, 11, 15}, target=9Output: index1=1 ...
- LeetCode 题解之 Two Sum
1.题目描述 2.问题分析 使用hashTable 寻找,target - num[i] ,将时间复杂度降低到 O(n): 3.代码 vector<int> twoSum(vector ...
- LeetCode题解之 two sum 问题
1.题目描述 2.题目分析 考虑使用hashMap的方式将数组中的每个元素和下表对应存储起来,然后遍历数组,计算target 和 数组中每个元素的差值,在hashMap中寻找,一直到找到最后一对. 3 ...
- [LeetCode题解]160. 相交链表 | 双指针 + 哈希表
方法一:双指针 解题思路 假设链表存在相交时,headA 的长度为 a + c,headB 的长度为 b + c.如果把 headA 连上 headB,headB 连上 headB 的话,当遍历这两个 ...
- Leetcode No.1 Two Sum(c++哈希表实现)
1. 题目 1.1 英文题目 Given an array of integers nums and an integer target, return indices of the two numb ...
- [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 ...
随机推荐
- 深入浅出 MappedByteBuffer
前言 java io操作中通常采用BufferedReader,BufferedInputStream等带缓冲的IO类处理大文件,不过java nio中引入了一种基于MappedByteBuffer操 ...
- 使用MATLAB一键制作mif文件
本文档主要讲解实现一个16384(2^14)点的14位正弦波数据mif格式文件的生成,使用此文件,我们便可以在FPGA上基于直接数字合成(DDS)原理生成标准的正弦波,即实现信号发生器的功能.关于DD ...
- Oracle EBS Model Function Technical
♡.Oracle EBS(ERP)Oracle 是公司名字,这个我估计大家都知道.EBS是E-Business Suite的缩写,简单的说,就是Oracle做的一个企业级的信息化软件或者系统,里面包含 ...
- Android-ImageUtils工具类
图片相关的工具类 public class ImageUtils { public static boolean saveImage(Bitmap photo, String spath) { try ...
- linux 常用命令,开发记住这些基本能够玩转linux
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- Solr中的一些查询参数
fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集.默认为 “*”,指所有的字段. defType: 指定query parser,常用defType=lucene, defType ...
- jenkin+docker+git持续集成环境搭建
1.安装Jenkins(需要在Jenkins容器中安装maven,java环境不用安装,Jenkins初次启动时会自动安装) 参考:docker中安装Jenkins 2.配置git 3.安装docke ...
- Java Been, EJB, POJO 之间的区别
Java Bean 是可复用的组件,对Java Bean并没有严格的规范,理论上讲,任何一个Java类都可以是一个Bean.但通常情况下,由于Java Bean是被容器所创建(如Tomcat)的,所以 ...
- 在Windows Server 2008上部署免费的https证书
背景 后web时代,https加密的重要性不言而喻.主流浏览器均对http站点标记不安全,敦促web服务提供商尽快升级至https. 原先的https证书多由各大域名服务商提供,动辄成千上万的部署证书 ...
- WM_COPYDATA+BHO+Qt实现进程间通信
最近项目有一个需求:点击网页上某个按钮,通知Qt客户端.网页相关操作使用了BHO,BHO与Qt通信通过WB_COPYDATA,为什么这么麻烦呢,因为项目正好用到了BHO,可能还有其他方式,能直接通过网 ...