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 ...
随机推荐
- vue中使用echarts画饼状图
echarts的中文文档地址:https://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20EC ...
- HDU 4393 Throw nails(贪心加模拟,追及问题)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=115361#problem/D 题意大致是:给出最多50000个人,拥有最初速度 ...
- 重读APUE(7)-link/unlink与mkdir/rmdir
link–用于创建一个现有文件的链接:实际上是新建一个目录项,指向当前文件的i节点: unlink–用于删除一个现有文件的连接:实际上是对引用i节点的目录项进行删除,并且对链接计数-1:系统会检查文件 ...
- python 调用github的api,呈现python的受欢迎的程度
1 使用api调用数据: 在浏览器的地址栏中输入: https://api.github.com/search/repositories?q=language:python&sort=star ...
- pip install staty
ERROR: Complete output from command python setup.py egg_info:ERROR: Traceback (most recent call last ...
- Qt编写数据可视化大屏界面电子看板10-改造QCustomPlot
一.前言 为了抛弃对QChart的依赖,以及echart的依赖,(当然,后期也会做qchart的版本和echart的版本,尤其是echart的版本是肯定会做的,毕竟echart的效果牛逼的一塌糊涂,全 ...
- ios 自动去重
//resultArrM 数据原//_indexArray 过滤后的数据//MYSelectAreaModel 模型 /* 重定义索引 */ - (void)sy_indexArray{ /* 索 ...
- 配置WEB错误页面
项目运行时,难免会出现错误,这些错误我们不可以也不方便直接让用户看到,所以配置错误页面是非常必要的. 一下是项目的Web.xml文件,在最下方阴影部分是配置错误界面. <?xml version ...
- clientdataset的使用
clientdataset的使用(一) Delphi做为一个快速应用开发工具,深受程序员的喜爱.其强大的组件功能,让程序员能够轻松. 高效地完成常见的界面开发.数据库应用等功能.然而,帮助的相对缺 ...
- 反向代理远端 单台tomcat 使用ip+端口
.环境 nginx 10.1.1.161 公网:123.58.251.166 tomcat 10.1.1.103 .tomcat 配置 [root@host---- ~]# netstat -tnlp ...