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 ...
随机推荐
- AGC024E Sequence Growing Hard
题意 给出\(n\),\(m\),\(mu\),问有多少个序列组\((A_0,A_1,\dots,A_n)\)满足: 序列\(Ai\)的长度恰好为\(i\) 所有元素均在\([1,m]\) \(A_{ ...
- Linux设备驱动程序 之 延迟执行
长延迟 有些驱动程序需要延迟比较长的时间,即长于一个时钟滴答: 忙等待 如果想把执行延迟若干个时钟滴答,或者对延迟的精度要求不高,最简单的实现方法就是一个监视jiffies计数器的循环:这种忙等待的实 ...
- mysql—并发控制及事务
并发控制 实现的并发访问的控制技术是基于锁: 锁分为表级锁和行级锁,MyISAM存储引擎不支持行级锁:InnoDB支持表级锁和行级锁: 锁的分类有读锁和写锁,读锁也被称为共享锁,加读锁的时候其他的人可 ...
- fastadmin 后台管理中,权限设置,不同管理员,显示不同的数据
1.https://doc.fastadmin.net/docs/controller.html
- 安装windows下安装mysql
参考文档:https://www.cnblogs.com/reyinever/p/8551977.html https://www.jb51.net/article/151213.htm 首先下载m ...
- TomCat概述
作用: * 用来接收客户端的请求 * 处理请求, 把动态资源转换成了静态资源(web容器) * 给客户端响应 服务器的分类: * weblogic: oracle公司大型的JavaEE服务器收费的 * ...
- Access restriction: The type JPEGImageEncoder is not accessible due to restriction
转: 解决办法:Access restriction: The type JPEGImageEncoder is not accessible due to restriction 2011年11月2 ...
- pip install staty
ERROR: Complete output from command python setup.py egg_info:ERROR: Traceback (most recent call last ...
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_11-webpack研究-npm和cnpm安装配置
node.js安装完成后,就自动安装了webpack. npm -v:查看npm安装的版本 当前安装目录默认的包 在node.js的目录下创建两个文件夹 这样路径就被修改成功了 cnpm npm in ...
- 使用rsync备份数据
(1).实验环境与目标 源主机:youxi1 192.168.5.101 目标主机:youxi2 192.168.5.102 目标:将源主机youxi1的数据备份到youxi2上. rsync是C/S ...