描述:

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的更多相关文章

  1. 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 ...

  2. LeetCode第一题:Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. [LeetCode]题1:two sum

      Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0 ...

  4. LeetCode 题解(一):Two Sum

    LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...

  5. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  6. [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. ...

  7. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  8. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. (Problem 13)Large sum

    Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072875339 ...

随机推荐

  1. Rails生成随机字符串及加解密

    1.生成安全的base64字符串 key = SecureRandom.urlsafe_base64

  2. 关于winform中子窗体最大化的问题

    在构建winform程序时,普遍的做法是将主窗体设置成 MDI Container,使它可以放置工作窗体(Form 而非 UserControl).工作窗体作为子窗体填充工作区,一般设置 FormBo ...

  3. 用户自定义类型《lua程序设计》 28章 笔记

    本实例实现一种很简单的类型------布尔数组.C语言可以实现将每个布尔值存储在一个bit中,从而减少内存用量. 必须的一些宏 Code Snippet #defineBITS_PER_WORD (C ...

  4. <转 >socket穿透代理代码(C++版)

    本文转自 http://blog.csdn.net/bodybo/article/details/7274865 写代码经常会遇到socket要通过代理连接服务器的情况,代理类型通畅有三种:HTTP. ...

  5. Flume、Kafka、Storm结合

    Todo: 对Flume的sink进行重构,调用kafka的消费生产者(producer)发送消息; 在Sotrm的spout中继承IRichSpout接口,调用kafka的消息消费者(Consume ...

  6. 9、Linux驱动的杂项设备

    杂项设备,是字符设备中的特殊,它的主设备号,是 10,不同的杂项设备,通过次设备号进行区分. 1.注册与注销 int misc_register(struct miscdevice * misc) 完 ...

  7. 预装WIN8改装WIN7之BIOS设置

    不少预装WIN8/10的朋友觉得WIN8/10不好用,想改装WIN7,可改装之后常常出现各种问题,甚至不能启动,往往是BIOS设置不当. 本文以联想小新V2000 预装WIN8.1中文版为例,说说WI ...

  8. HTML5事件-pageshow 和 pagehide

    <!doctype html> <html> <head> <title>html5事件</title> <meta charset= ...

  9. Spring简化配置

    一.提取公共配置 通过bean标签将公共的配置提取出来,然后指定abstract="true". 在其它bean标签中指定其parent就可以.

  10. CentOS 7中mariadb编译安装教程systemctl启动

    mariadb做为mysql的替代品 现在centos的新版本yum包已换成mariadb 跟上篇一样只是启动方式改为systemd 安装一些库 yum install gcc gcc-c++ wge ...