当前位置:网站首页>(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
边栏推荐
- Experiment 5: common automation libraries
- Laravel8 uses passport authentication to log in and generate a token
- MATLIB从excel表中读取数据并画出函数图像
- Unity 颜色板|调色板|无级变色功能
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- Oracle对表进行的常用修改命令
- Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
- Résumé des connaissances de gradle
- Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
- Nftscan Developer Platform launches Pro API commercial services
猜你喜欢
STM32通过串口进入和唤醒停止模式
公链与私链在数据隐私和吞吐量上的竞争
Gradle知识概括
Stop saying that microservices can solve all problems
快手的新生意,还得靠辛巴吆喝?
Can online reload system software be used safely? Test use experience to share with you
I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
Experiment 4: installing packages from Gui
若依请求url中带有jsessionid的解决办法
英国都在试行4天工作制了,为什么BAT还对996上瘾?
随机推荐
达晨史上最大单笔投资,今天IPO了
Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
The same job has two sources, and the same link has different database accounts. Why is the database list found in the second link the first account
Two week selection of tdengine community issues | phase II
A few suggestions for making rust library more beautiful! Have you learned?
Gradle knowledge generalization
Gpt-3 is a peer review online when it has been submitted for its own research
Knowledge * review
Newsletter L Huobi ventures is in-depth contact with genesis public chain
快手的新生意,还得靠辛巴吆喝?
亚朵三顾 IPO
MATLIB reads data from excel table and draws function image
Please help xampp to do sqlilab is a black
Spark Tuning (II): UDF reduces joins and judgments
How to find out if the U disk file of the computer reinstallation system is hidden
Cloud native (32) | kubernetes introduction to platform storage system
Experiment 4: installing packages from Gui
With the help of this treasure artifact, I became the whole stack
After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
Daily question brushing record (XV)