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 ...
随机推荐
- mysql not in用法
select * from zan where uid not in(select uid from zan where zhongjiang !=0) group by uid order by r ...
- js插件---tree(多级文件)插件如何使用
js插件---tree(多级文件)插件如何使用 一.总结 一句话总结:还是一般的引入js和css后js调用的方式, 只不过tree调用的时候必须设置一个 HTML 模板(就是调用的那段html代码,别 ...
- WHU 1470 Join in tasks 水题
http://acm.whu.edu.cn/land/problem/detail?problem_id=1470 大概是给你一个队列,每次移动队头的数到队尾并减1,如果本身这个数为1就删去. 然后a ...
- Linux网络编程--字节序
1 .谈到字节序,那么会有朋友问什么是字节序 非常easy:[比如一个16位的整数.由2个字节组成,8位为一字节,有的系统会将高字节放在内存低的地址上,有的则将低字节放在内存高的地址上,所以存在字节序 ...
- php excel文件导出之phpExcel扩展库
php Excel 文件导出 phpExcel 官网 http://phpexcel.codeplex.com/ /** * 导出特定文件 * 依据详细情况而定 */ public function ...
- asp.net 前后台数据交互方式(转)
https://blog.csdn.net/luckyrass/article/details/38758007 一.前台直接输出后台传递的数据 后台代码: // .aspx.cs public st ...
- codeforces 1037E. Trips(倒叙)
题目传送门: 解题思路: 正着搞好像有点恶心. 反着搞. 一边删一边搞,从崩坏的地方开始,入度--. 最后dfs崩坏,更新答案. 注意要把边删掉防止重复崩坏. 代码: #include<cstd ...
- Vue框架学习笔记
<div id="app"> </div> var app = new Vue({ el:"#app", // 绑定的元素 data:{ ...
- 洛谷 P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...
- Android提示版本号更新操作流程
Android提示版本号更新操作流程 2014年5月8日: andorid的app应用中都会有版本号更新的操作,今天空暇的时候就花了点心思弄了一下.主要技术方面用到了AsyncTask异步载入.htt ...