[leetCode][012] Two Sum (1)
【题目】:
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
【题意】:
题目中要求输入一个整形数组以及一个target,找出该整型数组中这样两个元素,使得这两个元素之和等于指定target的值。 题目中假设该两个元素必然存在,并且是只有一组(所以相对简单),返回的是这两个元素的index值(数组Index从1开始)。
【坑】:
我们拿到这个题目,首先能够想到的就是两次遍历,但是这样时间复杂度为O(n^2),无法通过leetCode的OJ测试。所以需要找其他方法。必然要小于O(n^2)复杂度才行。
【key point】:
我们经常会使用空间换取时间的方法来获取较小的时间复杂度。因此增加必要的数据结构来减少时间复杂度。这道题目一样,我们增加一个map结构,key为数组元素值,value为其在数组中对应的index。每次遍历到数组元素便去map结构中查找对应的补数,如果存在,那就说明找到了。如果没存在就记录当前元素以及其index,直到结束。
【解答】:
class Solution{
public:
// O(n) runtime, O(n) space
// We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.
std::vector<int> twoSum(std::vector<int>& numbers, int target){
std::vector<int> vecRet;
std::map<int, int> mapIndex;
for (size_t i = ; i < numbers.size(); ++i){
if ( != mapIndex.count(target - numbers[i])){
int nIndex = mapIndex[target - numbers[i]];
// 当前存储的Index肯定比i要小,注意要排除i
if (nIndex < i){
vecRet.push_back(nIndex + );
vecRet.push_back(i + );
return vecRet;
}
} else {
mapIndex[numbers[i]] = i;
}
}
return vecRet;
} // twoSum
};
【leetCode Submission】
【运行结果】:
希望各位看官不吝赐教,小弟感恩言谢~
[leetCode][012] Two Sum (1)的更多相关文章
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- 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 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [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 ...
- [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 ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- RO05 - 如何编写RemObjects SDK服务端 (Delphi Version)
转载:http://blog.csdn.net/henreash/article/details/2261134 本文档向你展示如何使用RemObjects(Delphi版)创建第一个服务.读了本文档 ...
- DataSnap 用TStream 传递大数据 返回流大小为-1的情况
DataSnap可以直接传递和返回TStream类型的参数,这点是很方便的.但是很多人发现好像大小稍微大点就工作不正常了,就变相使用其它类型转换来转换去,这样便利性就失去了. 官方有篇博客很详细的介绍 ...
- Linux 常用软件和服务的配置网站
http://www.linuxidc.com/Linux/2012-11/73862.htm Linux zabbix的安装和配置
- python 中接口的实现
实际上,由于python是动态语言,支持多继承,因此接口在语言层面,是没有的东东. 然后,在架构设计上,我们又需要这么一个东西,来规范开发人员的行为. 因此就有了zope.interface的诞生. ...
- The Perfect Stall (incomplete)
恩,一看就知道是一道二分图最大匹配的题. 感动得发现自己不会做..果然我是太弱了.学校里真是麻烦死,根本没有时间好吗. (NOIP)会不会感动地滚粗啊? 然后稍微看看,恩,匈牙利算法. 真是感动得落泪 ...
- 【Django】Django 直接执行原始SQL 如何防止SQL注入 ?
代码示例: #错误--不要直接格式化字符串 query = 'SELECT * FROM myapp_person WHERE last_name = %s' % lname Person.objec ...
- 取出type="button" 和type="text" 里面的值显示在页面
<script type="text/JavaScript> function changeLink() { document.getElementById("nod ...
- apache一个IP多个站点的配置方法
在日常的网站发布中很多情况下都无法做到一个IP对应一个站点,在IP4的情况下IP的资源是相对有限的.然而作为最流行的Apache自然也考虑到这种情况,那么接下来看看apache服务器怎么配置一个IP多 ...
- Android之ViewDragHelper
在自定义ViewGroup中,很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等),针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是 ...
- 多个div 一行显示的处理方式
1.方式一: 通过div的float属性,定义宽度,然后定义float属性和width的属性,实现多个div在一行显示: 2.方式二: 通过div的display的属性,至少进行2成div的displ ...