《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 ...
随机推荐
- Java反射API研究(4)——Class中的重要对象
一.Constructor与Method的父类:Executable Executable表示一个可执行类,构造方法与普通方法都是Executable AnnotatedType[] getAnnot ...
- Oracle EBS Patch Demo
Oracle EBS APP & DB 打补丁过程简述l例子: 打 Patch# 11843100:R12.CAC.B 打PATCH之前先查询一下是否已经有了这个PATCH. SELECT ...
- Android-Activity启动模式(launchMode)
Activity启动模式是非常重要的一块内容,启动模式直接关系到用户的体验 和 性能的提升等 Activity启动模式分为四种: 如果不配置:launchMode,默认就是:standard 标准的 ...
- Linux Context , Interrupts 和 Context Switching 说明
一. 进程Context 定义 当一个进程在执行时, CPU的所有寄存器中的值.进程的状态以及堆栈中的内容,比如各个变量和数据,包括所有的寄存器变量.进程打开的文件.内存信息等.这些信息被称为该进程的 ...
- java获取网络ip地址
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- C++多线程 生产者 消费者示例
之前写过一篇关于多线程的https://blog.csdn.net/qq_21049875/article/details/79589126. 为了复习一下C++多线程的使用,以及程序的编写,于是 ...
- Kafka与.net core(三)kafka操作
1.Kafka相关知识 Broker:即Kafka的服务器,用户存储消息,Kafa集群中的一台或多台服务器统称为broker. Message消息:是通信的基本单位,每个 producer 可以向一个 ...
- ClamAV资料链接
1.http://wiki.ubuntu.org.cn/index.php?title=ClamAV&variant=zh-cn Ubuntu的wiki下对ClamAV的大致介绍,包括使用. ...
- JAVA中-面向网络编程---单层交互
面向网络编程---单层交互: 客户端说明: /* * 实现TCP客户端,链接到服务器 * 和服务器实现数据交互 * 实现TCP客户端的类 java.net.Scoket * 构造方法: * Socke ...
- httpclient post请求例子(无参数名与带参数名的例子),多线程并发处理
版本:4.1 带参数名的情况 HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url ...