当前位置:网站首页>LeetCode 1. 两数之和
LeetCode 1. 两数之和
2022-07-02 13:15:00 【_刘小雨】
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> re;
unordered_map<int, int> hash; // 记录该数出现的位置
for(int i=0; i<nums.size(); i++)
{
int other = target - nums[i];
if(hash.count(other))
{
re = vector<int>({
hash[other], i});
break;
}
hash[nums[i]] = i;
}
return re;
}
};
边栏推荐
- Typescript array out of order output
- Student course selection system (curriculum design of Shandong Agricultural University)
- Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
- OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
- ROW_ NUMBER()、RANK()、DENSE_ Rank difference
- JS learning notes - first acquaintance
- Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
- How to use stustr function in Oracle view
- 绝对真理和相对真理思考
- Library management system (Shandong Agricultural University Curriculum Design)
猜你喜欢
Boot transaction usage
Summary | three coordinate systems in machine vision and their relationships
sim2real环境配置教程
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
Idea public method extraction shortcut key
Summary of multithreading and thread synchronization knowledge
随机推荐
Mysql database mysqldump why there is no statement to create a database
Invalid bound statement (not found) solution summary
SQLServer查询哪些索引利用率低
The login box of unity hub becomes too narrow to log in
数学分析_笔记_第5章:一元微分学
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
路由模式:hash和history模式
sql解决连续登录问题变形-节假日过滤
Another graduation season
AcWing 300. Task arrangement
总结|机器视觉中三大坐标系及其相互关系
忆当年高考|成为程序员的你,后悔了吗?
Library management system (Shandong Agricultural University Curriculum Design)
Recalling the college entrance examination and becoming a programmer, do you regret it?
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
云原生的 CICD 框架:Tekton
PCL 最小中值平方法拟合平面
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
Mobile web development learning notes - Layout
MySQL calculates the data within the longitude and latitude range