当前位置:网站首页>leetcode1-两数之和
leetcode1-两数之和
2020-11-10 08:39:00 【osc_nfjwhlc1】
题目描述
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
方法1
暴力枚举
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int length=nums.size();
vector<int> result;
for(int i=0;i<length;i++)
{
for(int j=i+1;j<length;j++)
{
if(target==nums[i]+nums[j])
{
result.push_back(i);
result.push_back(j);
break;
}
}
}
return result;
}
};
思路及算法
最容易想到的方法是枚举数组中的每一个数 x,寻找数组中是否存在 target - x。
当我们使用遍历整个数组的方式寻找 target - x 时,需要注意到每一个位于 x 之前的元素都已经和 x 匹配过,因此不需要再进行匹配。而每一个元素不能被使用两次,所以我们只需要在 x 后面的元素中寻找 target - x。
复杂度分析
- 时间复杂度:O(N^2)O(N2),其中 NN 是数组中的元素数量。最坏情况下数组中任意两个数都要被匹配一次。
- 空间复杂度:O(1)O(1)。
方法2
哈希表
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hashtable;
for (int i = 0; i < nums.size(); ++i) {
auto it = hashtable.find(target - nums[i]);
if (it != hashtable.end()) {
return {it->second, i};
}
hashtable[nums[i]] = i;
}
return {};
}
};
思路及算法
注意到方法一的时间复杂度较高的原因是寻找 target - x 的时间复杂度过高。因此,我们需要一种更优秀的方法,能够快速寻找数组中是否存在目标元素。如果存在,我们需要找出它的索引。
使用哈希表,可以将寻找 target - x 的时间复杂度降低到从 O(N)O(N) 降低到 O(1)O(1)。
这样我们创建一个哈希表,对于每一个 x,我们首先查询哈希表中是否存在 target - x,然后将 x 插入到哈希表中,即可保证不会让 x 和自己匹配。
复杂度分析
- 时间复杂度:O(N)O(N),其中 NN 是数组中的元素数量。对于每一个元素 x,我们可以 O(1)O(1) 地寻找 target - x。
- 空间复杂度:O(N)O(N),其中 NN 是数组中的元素数量。主要为哈希表的开销。
版权声明
本文为[osc_nfjwhlc1]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4312838/blog/4710498
边栏推荐
- csdn bug5:待加
- Ineuos industrial interconnection platform, web configuration (ineuview) increases the function of importing and exporting engineering views, as well as optimization and repair. Release: v3.2.1
- Overview of the most complete anomaly detection algorithm in history
- Android quick shutdown app
- Oschina: my green plants are potatoes, ginger and garlic
- 小度“破圈”提速,IoT迎来新故事
- 获取List集合对象中某一列属性值
- CSDN bug7: to be added
- The solution of polar experience insensitive verification
- CUDA_ Shared memory, memory access mechanism, access optimization
猜你喜欢

What's the difference between delete, truncate, and drop, and what to do if you delete data by mistake

Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop

About CentOS start error: the solution of failed to start crash recovery kernel arming

工厂方法模式

csdn bug7:待加

编码风格:Mvc模式下SSM环境,代码分层管理

Factory approach model

安卓快速关机APP

CSDN BUG1: to be added

Oschina: my green plants are potatoes, ginger and garlic
随机推荐
Fire knowledge online answer activity small program
Python cookbook 3rd note (2.1): using multiple qualifiers to split strings
JS solves the problem of automatic pagination in browser printing
《Python Cookbook 3rd》笔记(2.2):字符串开头或结尾匹配
The solution of polar experience insensitive verification
csdn bug11:待加
Incomplete Polyfill of proxy
Android quick shutdown app
关于centos启动报错:Failed to start Crash recovery kernel arming的解决方案
一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
After seven years of pursuing, nearly one billion US dollars of bitcoin was eventually confiscated and confiscated by the US government
csdn bug7:待加
编码风格:Mvc模式下SSM环境,代码分层管理
Coding style: SSM environment in MVC mode, code hierarchical management
CUDA_ constant memory
Prometheus installation configuration
CUDA_ Shared memory, memory access mechanism, access optimization
JMeter interface test -- a solution with token
Several solutions to the problem that selenium webdriver always fails to use click
Graph undirected graph