当前位置:网站首页>(LeetCode)两数之和
(LeetCode)两数之和
2022-07-06 15:58:00 【[email protected]】
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
提示:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案
进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?
Code:
class Solution
{
public:
// 输入:整数数组+整数目标值
// 输出:组成目标整数的两个数组元素的下标(任意顺序)
vector<int> twoSum(vector<int> &nums,int target)
{
// unordered_map基于哈希表实现,查找的时间复杂度较低O(1)
unordered_map<int,int> m;
// map基于红黑树实现,查找的时间复杂度O(n)
for(int i=0;i<nums.size();i++)
{
// 遍历,如果能组成目标整数,则输出下标
if(m.count(target-nums[i]))
{
return {m[target-nums[i]],i};
}
// 将前面查找过的数据存入unordered_map,以便与后面未查找过的数据匹配
m[nums[i]]=i;
}
// 未找到则返回空
return {};
}
};来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/two-sum
Reference:
C++ map和unordered_map的区别和联系以及map的使用_m0_67401660的博客-CSDN博客_unordered_map和map的区别
C++ unordered_map_永远爱好技术的王师傅的博客-CSDN博客_c++ unordered_map
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40728667/article/details/125586619
边栏推荐
- (1) Chang'an chain learning notes - start Chang'an chain
- 【无人机】多无人协同任务分配程序平台含Matlab代码
- Isomorphism + cross end, knowing applet +kbone+finclip is enough!
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- Daily question brushing record (XV)
- 借助这个宝藏神器,我成为全栈了
- 短链的设计
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- Automatically update selenium driver chromedriver
- COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
猜你喜欢

今日睡眠质量记录78分

【无人机】多无人协同任务分配程序平台含Matlab代码

谁说新消费品牌大溃败?背后有人赢麻了

【通信】两层无线 Femtocell 网络上行链路中的最优功率分配附matlab代码

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

leetcode:236. The nearest common ancestor of binary tree

Efficient ETL Testing

氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!

The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)

Isomorphism + cross end, knowing applet +kbone+finclip is enough!
随机推荐
Koa2 addition, deletion, modification and query of JSON array
DevSecOps软件研发安全实践——发布篇
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
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
Dockermysql modifies the root account password and grants permissions
让 Rust 库更优美的几个建议!你学会了吗?
(DART) usage supplement
本地部署 zeppelin 0.10.1
Realize colorful lines and shape your heart
Cover fake big empty talk in robot material sorting
Daily question brushing record (XV)
Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
Is the more additives in food, the less safe it is?
mysql连接vscode成功了,但是报这个错
Summary of three methods for MySQL to view table structure
问下各位,有没有flink sql生成作业的文档啊或是案列啊知道flink cli可以建表和指定目
How does crmeb mall system help marketing?
What does security capability mean? What are the protection capabilities of different levels of ISO?
With the help of this treasure artifact, I became the whole stack
基于PaddlePaddle平台(EasyDL)设计的人脸识别课堂考勤系统