当前位置:网站首页>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;
}
};
边栏推荐
- Leetcode --- longest public prefix
- Set the background picture in the idea (ultra detailed)
- 结构体的内存对齐
- IDEA中设置背景图片(超详细)
- Register as a harmonios developer and install deveco studio 3.0 beta2 for harmonios
- Recalling the college entrance examination and becoming a programmer, do you regret it?
- Typescript array out of order output
- [solution] educational codeforces round 82
- Boot transaction usage
- AWS云主机扩容
猜你喜欢
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
理想之光不灭
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
通过两级网关设计来路由服务网格流量
dried food! Understand the structural vulnerability of graph convolution networks
JS learning notes - first acquaintance
Idea public method extraction shortcut key
Typescript array out of order output
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
随机推荐
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Mobile web development learning notes - Layout
What is Amazon keyword index? The consequences of not indexing are serious
Compress words (kmp/ string hash, double hash)
Everyone Xinfu builds: a one-stop intelligent business credit service platform
Source code look me
[fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
通过两级网关设计来路由服务网格流量
数学分析_笔记_第6章:一元函数的Riemann积分
Best practices for building multi architecture images
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
sql解决连续登录问题变形-节假日过滤
(practice C language every day) the sum of the nearest three numbers
Vscode设置标签页多行显示
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
Which software is good for machine vision?
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
总结|机器视觉中三大坐标系及其相互关系
Summary | three coordinate systems in machine vision and their relationships
绝对真理和相对真理思考