当前位置:网站首页>(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
- Where does this "judge the operation type according to the op value and assemble SQL by yourself" mean? It means simply using Flink tab
- 新手问个问题,我现在是单机部署的,提交了一个sql job运行正常,如果我重启了服务job就没了又得
- JS import excel & Export Excel
- Local deployment Zeppelin 0.10.1
- 编译logisim
- How does crmeb mall system help marketing?
- Gpt-3 is a peer review online when it has been submitted for its own research
- STM32 enters and wakes up the stop mode through the serial port
- Ajout, suppression et modification d'un tableau json par JS
猜你喜欢
Who said that new consumer brands collapsed? Someone behind me won
Isomorphism + cross end, knowing applet +kbone+finclip is enough!
设计一个抢红包系统
Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
Entropy information entropy cross entropy
Daily question brushing record (XV)
每日刷题记录 (十五)
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
Server SMP, NUMA, MPP system learning notes.
随机推荐
MySQL implementation of field segmentation from one line to multiple lines of example code
A few suggestions for making rust library more beautiful! Have you learned?
leetcode:236. 二叉树的最近公共祖先
Common modification commands of Oracle for tables
编译logisim
公链与私链在数据隐私和吞吐量上的竞争
MATLIB reads data from excel table and draws function image
服务器SMP、NUMA、MPP体系学习笔记。
Talking about the current malpractice and future development
Restoration analysis of protobuf protocol of bullet screen in station B
(1) Chang'an chain learning notes - start Chang'an chain
STM32 enters and wakes up the stop mode through the serial port
Spark Tuning (II): UDF reduces joins and judgments
The programmer said, "I'm 36 years old, and I don't want to be rolled, let alone cut."
求帮助xampp做sqlilab是一片黑
Can online reload system software be used safely? Test use experience to share with you
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
CRMEB 商城系统如何助力营销?
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
The largest single investment in the history of Dachen was IPO today