LeetCode Algorithm 01_Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
Tags: Array, Hash Table
分析:本题容易想到使用双重循环遍历所有可能的组合,如何组合的两个数加起来等于Target,则返回。但是这样会超时。
那么,如何才可以降低算法复杂度呢?使用map。map内部自建一颗红黑树,可以对数据自动排序,正是因为map中数据有序,搜索起来会比较快。
这样,我们只需要单重循环遍历数组,对于每次遍历到的数numbers[i],我们在map中找有没有存储<Target-numbers[i], 相应index>数据对,这里键为Target-numbers[i],map排序是依据键值的。如果找到,则返回;如果没有找到,则在map种插入<numbers[i], i+1>作为map新数据对。
这样一来,每次遍历一个元素只需要在map种搜索对应元素,而不是像双重循环中那样,对每个元素都要尝试它后面的所有元素是否可以构成有效组合。
c++实现代码:
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> index;
map<int, int> mp;
map<int, int>::iterator iter;
for (int i = ; i != numbers.size(); ++i) {
iter = mp.find(target - numbers[i]);
if (iter != mp.end()) {
int start = mp.find(target - numbers[i])->second;
int end = i + ;
index.push_back(start);
index.push_back(end);
return index;
} else {
mp.insert(pair<int,int>(numbers[i],i+));
}
}
}
};
代码注释:
9.//find()函数返回一个迭代器,如果找到返回相应迭代器,如果没找到返回end()返回的迭代器
10.//找到了
11.//iter->first是map数据对的键值,iter->second是数据对的value
13.//vector的插入数据方式
16.//没找到
17.//将当前遍历到的值和相应index+1插入map。也可以通过 mp[numbers[i]] = i + 1 插入,但两者不同,insert是不覆盖,而此法覆盖。
LeetCode Algorithm 01_Two Sum的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- 【Uva 242】Stamps and Envelope Size
[Link]: [Description] 给你n个集合; 每个集合都包含一些不同面额的邮票; (每种邮票都当做有无限张) 然后给你一封信上最多能贴的邮票张数S; 问你,哪一个集合的邮票; 能够贴出来 ...
- Cocos2d-x开发的Android应用怎么加入插屏广告
Cocos2d-x系统开发游戏已经变得比較流行,但是用这个开发的游戏.想要加入广告就不是那么理想了.尤其是插屏广告.由于插屏广告通常是要在暂停或者结束游戏的时候展示才比較的合理.但是Cocos2d-x ...
- Reuse Is About People and Education, Not Just Architecture
 Reuse Is About People and Education, Not Just Architecture Jeremy Meyer you MigHT AdopT THE AppRoA ...
- PHP开发常规安全问题总结
文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/575 php给了开发人员极大的灵活性,可是这也为安全问题带来了潜在的隐患,最近须要总结一下 ...
- crm2011处理save事件和获取当前窗口信息
//防止保存记录,eContext:当前上下文对象 function My_PreventSaveFunction(eContext) { eContext.getEventArgs().pr ...
- DB2物化视图(Materialized Query Tables, MQT)
DB2的物化视图MQT是基于查询结果定义的一个表,MQT中包括的数据来自MQT定义所基于的一个或多个表, 使用MQT能够显著提高查询的操作性能. 数据库的视图和MQT都是基于一个查询来定义的.每当视图 ...
- js---14公有私有成员方法
var ns1 = {}; //命名空间 ns1.ns11 = {};//子命名空间 ns1.module1 = {name:"a",m:function(){}}; consol ...
- vim状态保存跟恢复
当我们结束了一天的工作的时候,可能手头的工作仅仅进行了一半,比如我们正在用vim修改一个android 问题,我们定位了问题关键,牵扯到了好几个类,如果这时候我们直接把vim关闭了,那我们下次还要重新 ...
- 56.ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
Node.js 在安装模块的时候报错,缺少python环境. ERR! configure error gyp ERR! stack Error: Can't find Python executab ...
- ocdefirst安装更新数据库
数据迁移(Migrations) 启用数据迁移在控制台中输入如下命令:Enable-Migrations 这时会在项目目录中增加一个Migrations文件夹,里面放置了两个文件:EF会通过C#代码的 ...