当前位置:网站首页>(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
边栏推荐
- Spark Tuning (II): UDF reduces joins and judgments
- The method of reinstalling win10 system is as simple as that
- One minute to learn how to install the system, win7 XP, win10 and win11 become very simple
- How much does the mlperf list weigh when AI is named?
- What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
- B站大佬用我的世界搞出卷積神經網絡,LeCun轉發!爆肝6個月,播放破百萬
- The same job has two sources, and the same link has different database accounts. Why is the database list found in the second link the first account
- 公链与私链在数据隐私和吞吐量上的竞争
- How does crmeb mall system help marketing?
- CRMEB 商城系统如何助力营销?
猜你喜欢

Today, I met a senior test developer from Tencent and saw the ceiling of the foundation

Résumé des connaissances de gradle
Docker starts MySQL and -emysql_ ROOT_ Password = my secret PW problem solving

GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议

(flutter2) as import old project error: inheritfromwidgetofexacttype

Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million

为了交通安全,可以做些什么?

Children's pajamas (Australia) as/nzs 1249:2014 handling process

Flutter life cycle

Master binary tree in one article
随机推荐
Gradle知識概括
设计一个抢红包系统
Computer reinstallation system teaching, one click fool operation, 80% of people have learned
实现多彩线条摆出心形
公链与私链在数据隐私和吞吐量上的竞争
MySQL implementation of field segmentation from one line to multiple lines of example code
Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
谁说新消费品牌大溃败?背后有人赢麻了
浅谈现在的弊端与未来的发展
请问oracle-cdc用JsonDebeziumDeserializationSchema反序列化
Talking about the current malpractice and future development
AcWing 4299. Delete point
mysql连接vscode成功了,但是报这个错
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
基于PaddlePaddle平台(EasyDL)设计的人脸识别课堂考勤系统
leetcode:236. 二叉树的最近公共祖先
短链的设计
[launched in the whole network] redis series 3: high availability of master-slave architecture
JS import excel & Export Excel
One minute to learn how to install the system, win7 XP, win10 and win11 become very simple