当前位置:网站首页>(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
边栏推荐
- 新手问个问题,我现在是单机部署的,提交了一个sql job运行正常,如果我重启了服务job就没了又得
- docker mysql5.7如何设置不区分大小写
- mysql连接vscode成功了,但是报这个错
- Experiment 5: common automation libraries
- Flutter life cycle
- Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
- Experiment 6: installing eve-ng
- (flutter2) as import old project error: inheritfromwidgetofexacttype
- Cloud native (32) | kubernetes introduction to platform storage system
- MySQL connected vscode successfully, but this error is reported
猜你喜欢
浅谈现在的弊端与未来的发展
每日刷题记录 (十五)
MySQL数据库之JDBC编程
leetcode:236. 二叉树的最近公共祖先
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
传统企业要为 Web3 和去中心化做的 11 个准备
今日睡眠质量记录78分
[launched in the whole network] redis series 3: high availability of master-slave architecture
js對JSON數組的增删改查
Cover fake big empty talk in robot material sorting
随机推荐
What does security capability mean? What are the protection capabilities of different levels of ISO?
【通信】两层无线 Femtocell 网络上行链路中的最优功率分配附matlab代码
CRMEB 商城系统如何助力营销?
Computer reinstallation system teaching, one click fool operation, 80% of people have learned
Cover fake big empty talk in robot material sorting
每日刷题记录 (十五)
云原生(三十二) | Kubernetes篇之平台存储系统介绍
Docker mysql5.7 how to set case insensitive
(flutter2) as import old project error: inheritfromwidgetofexacttype
石墨文档:4大对策解决企业文件信息安全问题
若依请求url中带有jsessionid的解决办法
AI金榜题名时,MLPerf榜单的份量究竟有多重?
With the help of this treasure artifact, I became the whole stack
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
Example code of MySQL split string as query condition
MySQL implementation of field segmentation from one line to multiple lines of example code
谁说新消费品牌大溃败?背后有人赢麻了
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
Modules that can be used by both the electron main process and the rendering process
Gradle知识概括