当前位置:网站首页>(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
边栏推荐
- Cover fake big empty talk in robot material sorting
- flinksql select id ,count(*) from a group by id .
- Win11怎么恢复传统右键菜单?Win11右键改回传统模式的方法
- Experiment 4: installing packages from Gui
- TDengine 社区问题双周精选 | 第二期
- Efficient ETL Testing
- 内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)
- What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
- 机器人材料整理中的套-假-大-空话
- 为了交通安全,可以做些什么?
猜你喜欢
Please help xampp to do sqlilab is a black
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
Restoration analysis of protobuf protocol of bullet screen in station B
同构+跨端,懂得小程序+kbone+finclip就够了!
Résumé des connaissances de gradle
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
Gradle知識概括
None of the strongest kings in the monitoring industry!
Efficient ETL Testing
英国都在试行4天工作制了,为什么BAT还对996上瘾?
随机推荐
What does security capability mean? What are the protection capabilities of different levels of ISO?
flinksql select id ,count(*) from a group by id .
【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
石墨文档:4大对策解决企业文件信息安全问题
MySQL数据库之JDBC编程
Ajout, suppression et modification d'un tableau json par JS
基于PaddlePaddle平台(EasyDL)设计的人脸识别课堂考勤系统
Efficient ETL Testing
达晨史上最大单笔投资,今天IPO了
Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
Experiment 4: installing packages from Gui
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
AI金榜题名时,MLPerf榜单的份量究竟有多重?
What can be done for traffic safety?
Devsecops software R & D security practice - release
With the help of this treasure artifact, I became the whole stack
The method of reinstalling win10 system is as simple as that
Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
Scholar doctor hahaha
Gpt-3 is a peer review online when it has been submitted for its own research