当前位置:网站首页>(leetcode) sum of two numbers
(leetcode) sum of two numbers
2022-07-06 23:43:00 【[email protected]】
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
Tips :
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer
Advanced : You can come up with a time complexity less than O(n2) The algorithm of ?
Code:
class Solution
{
public:
// Input : An array of integers + Integer target
// Output : Subscripts of the two array elements that make up the target integer ( In any order )
vector<int> twoSum(vector<int> &nums,int target)
{
// unordered_map Implementation based on hash table , The time complexity of searching is low O(1)
unordered_map<int,int> m;
// map Based on the red black tree , The time complexity of finding O(n)
for(int i=0;i<nums.size();i++)
{
// Traverse , If you can form a target integer , Output subscript
if(m.count(target-nums[i]))
{
return {m[target-nums[i]],i};
}
// Save the data found above unordered_map, In order to match the data not found later
m[nums[i]]=i;
}
// If not found, null is returned
return {};
}
}; source : Power button (LeetCode)
link :https://leetcode.cn/problems/two-sum
Reference:
C++ map and unordered_map The differences and connections between map Use _m0_67401660 The blog of -CSDN Blog _unordered_map and map The difference between
C++ unordered_map_ The blog of Master Wang who always loves Technology -CSDN Blog _c++ unordered_map
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061558379637.html
边栏推荐
- 公链与私链在数据隐私和吞吐量上的竞争
- Design of short chain
- Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
- JS addition, deletion, modification and query of JSON array
- 自动更新Selenium驱动chromedriver
- (LeetCode)两数之和
- Oracle对表进行的常用修改命令
- There are only two TXT cells in the ArrayExpress database. Can you only download the sequencing run matrix from line to ENA?
- 基础图表解读“东方甄选”爆火出圈数据
- 达晨史上最大单笔投资,今天IPO了
猜你喜欢

B站大佬用我的世界搞出卷積神經網絡,LeCun轉發!爆肝6個月,播放破百萬

Who said that new consumer brands collapsed? Someone behind me won

零代码高回报,如何用40套模板,能满足工作中95%的报表需求

The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly

GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议

今日睡眠质量记录78分

Gold three silver four, don't change jobs

STM32通过串口进入和唤醒停止模式

Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application

With the help of this treasure artifact, I became the whole stack
随机推荐
Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
Computer reinstallation system teaching, one click fool operation, 80% of people have learned
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
Common modification commands of Oracle for tables
Local deployment Zeppelin 0.10.1
【无人机】多无人协同任务分配程序平台含Matlab代码
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
How does crmeb mall system help marketing?
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
How to find out if the U disk file of the computer reinstallation system is hidden
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
英国都在试行4天工作制了,为什么BAT还对996上瘾?
B 站弹幕 protobuf 协议还原分析
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
Two week selection of tdengine community issues | phase II
The programmer refused the offer because of low salary, HR became angry and netizens exploded
Talking about the current malpractice and future development
【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码