当前位置:网站首页>Niuke.com: sum of two numbers
Niuke.com: sum of two numbers
2022-06-10 22:45:00 【lsgoose】


When I saw this question, my first reaction was to do pair, Number and index . Then sort the numbers , Then double pointer .
It turns out that there is something as magical as a hash table . So we can use a hash table to store values and indexes .
Ideas as follows :
Traverse the entire array :
1. Complement the current number to get temp
2.temp Not in hash table , Then put the current number and its index into the hash table
3. otherwise , Find two numbers , Add... To the index 1 Put the label of into the answer , end
class Solution {
public:
/**
*
* @param numbers int integer vector
* @param target int integer
* @return int integer vector
*/
vector<int> twoSum(vector<int>& numbers, int target) {
// write code here
vector<int> res;
unordered_map<int, int> hash;
for(int i=0;i<numbers.size();++i){
int temp=target-numbers[i];
if(hash.find(temp)==hash.end()){
hash[numbers[i]]=i;
}else{
res.push_back(hash[temp]+1);
res.push_back(i+1);
break;
}
}
return res;
}
};边栏推荐
- SQL Server查询区分大小写
- Record (III)
- TcaplusDB君 · 行业新闻汇编(六)
- Matlab - 演化博弈论实现
- Model construction of mmdetection
- TcaplusDB君 · 行业新闻汇编(三)
- Sealem Finance打造Web3去中心化金融平台基础设施
- [tcapulusdb knowledge base] Introduction to tcapulusdb patrol inspection statistics
- SQL exercise 4: string processing function
- TcaplusDB君 · 行业新闻汇编(五)
猜你喜欢

Whale conference empowers intelligent epidemic prevention
![[tcapulusdb knowledge base] tcapulusdb shard relocation introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb shard relocation introduction

鲸会务智慧景区管理解决方案

Differences between disk serial number, disk ID and volume serial number

1.Tornado简介&&本专栏搭建tornado项目简介

Matlab - 演化博弈论实现

TcaplusDB君 · 行业新闻汇编(四)

【TcaplusDB知识库】TcaplusDB巡检统计介绍

鲸会务会议分享:大会难办怎么办?
![[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration](/img/98/0819d5c30a3f214d97b43f2a949fa2.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
随机推荐
[tcapulusdb knowledge base] tcapulusdb transaction management introduction
Web3 ecological decentralized financial platform sealem Finance
[debug] could not find ref wiht POC XXX
Pytorch 安装超简单
自己搞了一个相亲软件的源码,用兴趣的可以聊聊
Mmdetection dataloader construction
TcaplusDB君 · 行业新闻汇编(三)
Model construction of mmdetection
【TcaplusDB知识库】TcaplusDB刷新tbus通道介绍
Add, delete, query and modify MySQL table structure (DDL)
I made a blind date software source code, and I can chat with those who are interested
图像拼接摄像头拼接笔记
[XPath] use following sibling to obtain the following peer nodes
What about the popular state management library mobx?
leetcode 130. Surrounded regions (medium)
Notes (II)
【TcaplusDB知识库】TcaplusDB推送配置介绍
CCF CSP 202109-1数组推导
Management solution for whale conference smart scenic spot
[tcapulusdb knowledge base] tcapulusdb viewing process status introduction