当前位置:网站首页>(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)
- 请问oracle-cdc用JsonDebeziumDeserializationSchema反序列化
- 基于PaddlePaddle平台(EasyDL)设计的人脸识别课堂考勤系统
- 自动更新Selenium驱动chromedriver
- js對JSON數組的增删改查
- 浅谈现在的弊端与未来的发展
- How can Oracle CDC deserialize with jsondebeziumdeserializationschema
- A few suggestions for making rust library more beautiful! Have you learned?
- Is the more additives in food, the less safe it is?
- js对JSON数组的增删改查
猜你喜欢

人均瑞数系列,瑞数 4 代 JS 逆向分析

leetcode:236. The nearest common ancestor of binary tree

The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method

(shuttle) navigation return interception: willpopscope

Talking about the current malpractice and future development

Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~

The programmer refused the offer because of low salary, HR became angry and netizens exploded

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

Pdf batch splitting, merging, bookmark extraction, bookmark writing gadget

【无人机】多无人协同任务分配程序平台含Matlab代码
随机推荐
Face recognition class attendance system based on paddlepaddle platform (easydl)
Efficient ETL Testing
Summary of three methods for MySQL to view table structure
flinksql select id ,count(*) from a group by id .
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
A few suggestions for making rust library more beautiful! Have you learned?
Experiment 6: installing eve-ng
Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
js对JSON数组的增删改查
Knowledge * review
本地部署 zeppelin 0.10.1
Detailed explanation of regular expression (regexp) in MySQL
spark调优(二):UDF减少JOIN和判断
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
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
JS import excel & Export Excel
每日刷题记录 (十五)