当前位置:网站首页>(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
边栏推荐
- If the request URL contains jsessionid, the solution
- Common modification commands of Oracle for tables
- js對JSON數組的增删改查
- 今日睡眠质量记录78分
- 快讯 l Huobi Ventures与Genesis公链深入接洽中
- Gradle knowledge generalization
- How much does the mlperf list weigh when AI is named?
- Realize colorful lines and shape your heart
- Scholar doctor hahaha
- The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
猜你喜欢

Wu Enda 2022 machine learning course evaluation is coming!

B 站弹幕 protobuf 协议还原分析

Newsletter L Huobi ventures is in-depth contact with genesis public chain

Penetration test --- database security: detailed explanation of SQL injection into database principle

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

Koa2 addition, deletion, modification and query of JSON array

Server SMP, NUMA, MPP system learning notes.

Nftscan Developer Platform launches Pro API commercial services
Detailed explanation of regular expression (regexp) in MySQL

After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
随机推荐
Entropy information entropy cross entropy
mysql-cdc 的jar包 ,在flink运行模式下,是不是要放在不同的地方呢?
Summary of three methods for MySQL to view table structure
Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
How does crmeb mall system help marketing?
MySQL connected vscode successfully, but this error is reported
自动更新Selenium驱动chromedriver
请问async i/o可以由udf算子实现然后用sql api调用吗?目前好像只看到Datastre
The programmer refused the offer because of low salary, HR became angry and netizens exploded
每日刷题记录 (十五)
Gold three silver four, don't change jobs
Spark Tuning (II): UDF reduces joins and judgments
A novice asks a question. I am now deployed on a single machine. I submitted an SQL job and it runs normally. If I restart the service job, it will disappear and I will have to
若依请求url中带有jsessionid的解决办法
Experiment 4: installing packages from Gui
同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
Gpt-3 is a peer review online when it has been submitted for its own research
js导入excel&导出excel
STM32通过串口进入和唤醒停止模式