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

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

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

  6. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  7. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  8. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  9. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

随机推荐

  1. PHP的数组分为两种类型,一种是索引数组,一种是关联数组。有如下关联数组,我们如何获取它的第一个key和value呢?

    示例:$items=array('name'=>'sjm','age'=>'26','sex' => '男','location'=>'北京'); //当然用循环然后break ...

  2. 【VC++学习笔记四】MFC应用程序中框架类的获取

    一.文档类中 获取视图: 先获取主窗体,在根据主窗体获取视图 pMain->GetActiveDocument();注意类型转换 由于文档中可能包含多个视图,可以按照下面函数获取: CView* ...

  3. 开创学习的四核时代-iTOP-4412开发板开源硬件平台

    iTOP-4412开发板如今比較热门的开发板.笔者最近入了一套. 也推荐给初学ARM的朋友学习,4412开发板搭载三星Exynos四核处理器,配备1GB内存,4GB固态硬盘EMMC存储,兼具高速读取与 ...

  4. jQuery Easy UI ProgressBar(进度条)组件

    ProgressBar(进度条)组件,这个还是挺好玩的.我们在自己做点什么的时候常常能用到,比方上传下载文件.导入导出文档啊.加载网页等等. 应用场景非常多,使用起来还非常easy. 演示样例: &l ...

  5. 稀疏表示字典的显示(MATLAB实现代码)

    本文主要是实现论文--基于稀疏表示的图像超分辨率<Image Super-Resolution Via Sparse Representation>中的Figure2.通过对100000个 ...

  6. jq---方法总结

    1. 什么是jQuery 在使用jQuery之前,我们必须先了解什么是jQuery,它能够干什么(不然我们为啥要用它). jQuery是一个非常流行的快速.小巧.功能强大的开源JavaScript库. ...

  7. $.each(data, function (index, value) { })的用法;json和list<>的互相转换

    在json中常常碰到这样的代码: jquery $.each(data, function (index, value) {    }) 遍历处理data,可以是数组.DOM.json等,取决于直接给 ...

  8. ie中 专有的注释

    http://www.cnblogs.com/liluping860122/p/3539165.html

  9. 制作 wordpress 博客静态化到本地

    wget 克隆 wordpress 博客镜像 wget -e robots=off -w 1 -xq -np -nH -pk -m -t 1 -P "./wordpress.org" ...

  10. 如果笔记本的 WIN7 运行很卡,请尝试运行这些批处理

    如果笔记本的 WIN7 运行很卡,请尝试运行这些批处理 WIN7是不是很卡?关掉下列服务吧 @echo off rem AppXSvc 为部署应用商店应用程序提供基础结构支持 rem BITS 微软的 ...