描述:

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. C# 关键字 Visual Studio 2012

    C# 关键字 Visual Studio 2012 其他版本 关键字是对编译器具有特殊意义的预定义保留标识符. 它们不能在程序中用作标识符,除非它们有一个 @ 前缀. 例如,@if 是有效的标识符,但 ...

  2. XAMPP Apache + MySQL + PHP + Perl

    XAMPP Apache + MySQL + PHP + Perl 什么是XAMPP? XAMPP是最流行的PHP开发环境 XAMPP是完全免费且易于安装的Apache发行版,其中包含MySQL.PH ...

  3. MAC下MySQL忘记初始密码

    MAC下MySQL忘记初始密码的解决方法分享给大家,供大家参考,具体内容如下 从官网安装好MySQL的dmg后. 1 设置mysql命令 从终端输入 ? 1 mysql --version 若显示版本 ...

  4. 【BIEE】18_时间序列函数的使用

    三个时间序列函数 AGO: 实现同环比 TO DATE:实现累计指标,如MTD月累计.YTD年累计 Period Rolling:当前时间的x个时间单位开始到y个时间单位结束这一时段内的度量总和 BI ...

  5. 【Android进阶】怎样使用文件来保存程序中的数据

    在程序中.有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作 我直接写了一个帮助类,进行文件的写入和读取操作 /** * 用于在文件里保存程序数据 * * ...

  6. VS2017、netcore版本更新升级

    VS2017 剩下的就是下一步了. netcore 访问:https://www.microsoft.com/net/download/archives 找到对应版本(最新版本) 下载安装就可以了 装 ...

  7. Codeforces 476C Dreamoon and Sums (水

    题目链接:点击打开链接 题意: 给定a,b 对于一个数x.若x是nice number,则满足(x/b)/(x%b) == [1,a](即结果在1-a之间) 问: 输出一个数表示 全部nice num ...

  8. Atitit.数据库表的物理存储结构原理与架构设计与实践

    Atitit.数据库表的物理存储结构原理与架构设计与实践 1. Oracle和DB2数据库的存储模型如图: 1 1.1. 2. 表数据在块中的存储以及RowId信息3 2. 数据表的物理存储结构 自然 ...

  9. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  10. Python内置函数之input()

    input([prompt])input()读取标准输入并打印字符串到屏幕. 参数是自定义的提示符. 例子: >>> input('$ ') $ pwd 'pwd'