当前位置:网站首页>(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
边栏推荐
- MATLIB reads data from excel table and draws function image
- 每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
- Two week selection of tdengine community issues | phase II
- The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
- 这个『根据 op 值判断操作类型来自己组装 sql』是指在哪里实现?是指单纯用 Flink Tabl
- 每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报...
- Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
- 氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
- Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
- How can Oracle CDC deserialize with jsondebeziumdeserializationschema
猜你喜欢

达晨史上最大单笔投资,今天IPO了

What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
Detailed explanation of regular expression (regexp) in MySQL

快讯 l Huobi Ventures与Genesis公链深入接洽中

资产安全问题或制约加密行业发展 风控+合规成为平台破局关键

基础图表解读“东方甄选”爆火出圈数据

Today's sleep quality record 78 points

Gpt-3 is a peer review online when it has been submitted for its own research

电脑重装系统u盘文件被隐藏要怎么找出来

Efficient ETL Testing
随机推荐
Gradle knowledge generalization
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
The programmer refused the offer because of low salary, HR became angry and netizens exploded
Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.
How can Oracle CDC deserialize with jsondebeziumdeserializationschema
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
Unity color palette | color palette | stepless color change function
Unity 颜色板|调色板|无级变色功能
Newsletter L Huobi ventures is in-depth contact with genesis public chain
内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)
JDBC programming of MySQL database
Yaduo Sangu IPO
(1) Chang'an chain learning notes - start Chang'an chain
MATLIB reads data from excel table and draws function image
Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
浅谈现在的弊端与未来的发展
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
新手问个问题,我现在是单机部署的,提交了一个sql job运行正常,如果我重启了服务job就没了又得
请问async i/o可以由udf算子实现然后用sql api调用吗?目前好像只看到Datastre
不要再说微服务可以解决一切问题了