LC 1. Two Sum
题目介绍
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
参考答案
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> hp;
int n = nums.size();
vector<int> res;
for(int i=;i<n;i++){
int rest = target - nums[i];
if(hp.find(rest) != hp.end()){
res.push_back(hp[rest]);
res.push_back(i);
return res;
}
hp[nums[i]] = i; // map[key] = value -> map[num] = index;
}
return res;
}
};
补充说明
循环分成三部分:
1. 剩下的 = 目标 - 现在的
2. 检查剩下的是否在map里,在的话,就说明是数组里面的数字,可以返回index了。
3. 将该数字存入map中
有一些要特别注意的是:
map[ key ] = value -> map[num] = index ,以数字为key, 以存入的内容为index。所以,搜索的时候,以key(即数字)为依据,而 index 为我们要的结果。
LC 1. Two Sum的更多相关文章
- LC 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- LC 494. Target Sum
问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...
- LC 813. Largest Sum of Averages
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LC 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LC 677. Map Sum Pairs
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- [LC] 1099. Two Sum Less Than K
Given an array A of integers and integer K, return the maximum S such that there exists i < j wit ...
- [LC] 167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LC] 437. Path Sum III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LC] 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- Screen 用法简述
Screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换.GNU Screen可以看作是窗口管理器的命令行界面版本.它提 ...
- C语言学习笔记5-程序结构
本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/50752148 作者:jadeshu 邮箱: jades ...
- laotech老师唠科mac 深入浅出MAC OS X ceshi ruguokeyi
laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...
- 【原创】CancellableWait
应用程序不能正常退出,导致无法关机,这种情况通常是应用程序在等待一些I/O request to finish. 应用程序访问远程文件时,这种情况的发生更加频繁. If an application ...
- 《你不知道的JavaScript(上)》笔记——动态作用域
动态作用域让作用域作为一个在运行时就被动态确定的形式, 而不是在写代码时进行静态确定的形式.动态作用域并不关心函数和作用域是如何声明以及在何处声明的, 只关心它们从何处调用. 换句话说, 作用域链是基 ...
- ubuntu 命令行以及目录的显示改为英文
在安装ubuntu的时候原本以为把ubuntu改为中文的好理解一些,最后发现命令行的报错一边中文一边英文确实难受,也不宜于学习,所以想把命令行以及目录的显示都改为英文的,我是这样先把命令行的该为英文的 ...
- Java动态修改运行环境
1.pom.xml直接添加一下配置 <profiles> <profile> <id>dev</id> <properties> <a ...
- jenkins打完包在哪里
查看控制台输出最后面一行是jar包的位置 [JENKINS] Archiving /var/lib/jenkins/jobs/moven-test/workspace/target/projectA- ...
- swift 第五课 定义model类 和 导航栏隐藏返回标题
1. 网络请求返回数据时候,把数据转化为model,但是有时候会返回空的字符串,所以加载了个长度的判断: class Model : NSObject{ var details_url:String? ...
- Nginx-windows
1.下载 http://nginx.org/ 选择最新稳定版本,例如nginx-1.15.5 mainline version has been released. 点击后,跳转页面,选择Stable ...