LeetCode Problem 2: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
思路1:暴力搜索,两层循环。时间:O(n^2),空间:O(1),可能出现超时
思路2:考虑使用map或者hash_map,遍历输入的numbers,在map映射表中寻找target-numbers[i],C++ STL中map使用红黑树实现,查找元素时间O(logn),总时间O(nlogn)
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> result;
map<int, int> hashMap;
for(int i = ; i < numbers.size(); i++) {
if(!hashMap.count(numbers[i]))
hashMap.insert(pair<int,int>(numbers[i], i)); // <value, key>
if(hashMap.count(target - numbers[i])) {
int n = hashMap[target - numbers[i]]; //get the second number's position
if(n < i) {
result.push_back(n + );
result.push_back(i + );
return result;
}
//unnecessary
/*if(n > i) {
result.push_back(i + 1);
result.push_back(n + 1);
return result;
}*/
}
}
}
};
附LeetCode建议解决方案

LeetCode Problem 2:Two Sum的更多相关文章
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
- LeetCode第一题:Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode]题1:two sum
Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0 ...
- LeetCode 题解(一):Two Sum
LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [LeetCode] 327. Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- (Problem 13)Large sum
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072875339 ...
随机推荐
- Quartz定时框架入门
Quartz框架是Java开源的定时任务调度器,Quartz框架中有如下核心概念: 1. Job 任务接口,接口中只声明方法void execute(JobExecutionContext conte ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何设置PLC的扫描周期,运行周期
双击PlcTask,然后再Cycle ticks中可以修改PLC的扫描周期,例如修改为2ms 为了验证是否真的是2ms,可以在程序中跟计数器绑定使用,PLC2ms扫描一次,计数器也是每个周期增加0 ...
- FastGUI for NGUI教程
原地址:http://blog.csdn.net/asd237241291/article/details/8499430 FastGUI是NGUI的一个扩展,所以必须要有NGUI才能使用.FastG ...
- 放大的X 【杭电-2655】 附题
/* 放大的X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 预装WIN8改装WIN7之BIOS设置
不少预装WIN8/10的朋友觉得WIN8/10不好用,想改装WIN7,可改装之后常常出现各种问题,甚至不能启动,往往是BIOS设置不当. 本文以联想小新V2000 预装WIN8.1中文版为例,说说WI ...
- mvc5整合Autofac
本文中将使用 mvc5与webapi2进行对Autofac整合 准备工作: 1.vs2013 or vs2013+ 2.网络良好,nuget正常访问 好了需要的准备工作就这么多. ---------- ...
- es number_of_shards和number_of_replicas
number_of_replicas 是数据备份数,如果只有一台机器,设置为0 number_of_shards 是数据分片数,默认为5,有时候设置为3 可以在线改所有配置的参数,number_of ...
- Webpack 的 HtmlWebpackPlugin 如何控制某个 chunks 的 inject 位置?
https://segmentfault.com/q/1010000006591131 通过修改 HtmlWebpackPlugin 源码实现了 修改后的配置: new HtmlWebpackPlug ...
- Linux tomcat安装详解(未完)
转: http://blog.csdn.net/lcyaiym/article/details/76696192
- unity, 内存profile,ImageEffects Temp和Unity GI SystemTex RGBM
最近用unity的Profiler对公司项目进行内存profile,发现一些问题,记录一下. 用Memory Area的Detailed View,用法见:http://docs.unity3d.co ...