LeetCode Algorithm 01_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
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的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- [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 ...
- 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][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [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 ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- Linux学习总结(6)——CenterOS7安装mysql5.5的方法
首先centos7 已经不支持mysql,因为收费了你懂得,所以内部集成了mariadb,而安装mysql的话会和mariadb的文件冲突,所以需要先卸载掉mariadb,以下为卸载mariadb,安 ...
- qt qlineedit只输入数字
lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该 ...
- js---13 this call apply
//this:this可以写在js全剧环境中(全局环境中this是window对象),this可以写在函数中,可以写在对象中, function f(){ this.name = "asdf ...
- Glide二次封装库的使用
更多代码可以查询本人GitHub:欢迎阅读,star点起来. Glide二次封装库源码 前言 为什么选择Glide? Glide 轻量级 速度快 可以根据所需加载图片的大小自动适配所需分辨率的图 支持 ...
- javafx Cursor
public class EffectTest extends Application { ObservableList cursors = FXCollections.observableArray ...
- 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- Ansible学习记录三:配置文件
0.配置文件 两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下. ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径.模块 ...
- wget---从指定的URL下载文件
wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服务器打断下 ...
- 使用spring-boot 国际化配置所碰到的乱码问题
写好html静态页面 , 也加上了编码格式 , 获取国际化展示在浏览器中还是存在乱码 , 开始以为是浏览器编码格式问题 , 做过处理后任没有得到解决 , 具体的处理方案如下: <meta ht ...