当前位置:网站首页>(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
边栏推荐
- Face recognition class attendance system based on paddlepaddle platform (easydl)
- 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
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- Docker mysql5.7 how to set case insensitive
- Computer reinstallation system teaching, one click fool operation, 80% of people have learned
- Graphite document: four countermeasures to solve the problem of enterprise document information security
- Pdf batch splitting, merging, bookmark extraction, bookmark writing gadget
- AcWing 4299. Delete point
- 前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
- DevSecOps软件研发安全实践——发布篇
猜你喜欢

Nftscan Developer Platform launches Pro API commercial services

Résumé des connaissances de gradle

基础图表解读“东方甄选”爆火出圈数据

How to implement Lua entry of API gateway

What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?

【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码

asp读取oracle数据库问题

Entropy information entropy cross entropy

传统企业要为 Web3 和去中心化做的 11 个准备
Detailed explanation of regular expression (regexp) in MySQL
随机推荐
Entropy information entropy cross entropy
Ajout, suppression et modification d'un tableau json par JS
Restoration analysis of protobuf protocol of bullet screen in station B
【无人机】多无人协同任务分配程序平台含Matlab代码
Experiment 5: common automation libraries
AI金榜题名时,MLPerf榜单的份量究竟有多重?
Master binary tree in one article
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
Experiment 6: installing eve-ng
The programmer refused the offer because of low salary, HR became angry and netizens exploded
Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
docker mysql5.7如何设置不区分大小写
借助这个宝藏神器,我成为全栈了
Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.
食品里的添加剂品种越多,越不安全吗?
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
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
Where does this "judge the operation type according to the op value and assemble SQL by yourself" mean? It means simply using Flink tab
[launched in the whole network] redis series 3: high availability of master-slave architecture