当前位置:网站首页>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
边栏推荐
- 一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
- 编码风格:Mvc模式下SSM环境,代码分层管理
- The kth smallest node in the print binary search tree of offer
- The length of the last word in leetcode
- Yixian e-commerce prospectus of perfect diary parent company: focusing on marketing and ignoring R & D, with a loss of 1.1 billion in the first three quarters
- 痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
- Prometheus installation configuration
- csdn bug4:待加
- Hong Kong listed companies transfer cards to acquire 42.5% equity of chuangxinzhong and plan to speed up the distribution of marketing services
- 实验2
猜你喜欢
JS label syntax jumps out of multiple loops
CUDA_ Shared memory, memory access mechanism, access optimization
Overview of the most complete anomaly detection algorithm in history
关于centos启动报错:Failed to start Crash recovery kernel arming的解决方案
Problems and solutions in configuring FTP server with FileZilla server
极验无感验证破解
Notes on Python cookbook 3rd (2.2): String start or end match
One image can hold 16x16 words! ——Transformers for large scale image scaling recognition (a brief review of ICLR 2021 papers)
编码风格:Mvc模式下SSM环境,代码分层管理
If you need a million objects
随机推荐
YouTube subscription: solve the problem of incomplete height display of YouTube subscription button in pop-up window
Simple use of JMeter
Difficulties in heterogeneous middleware implementation of Bifrost site management (1)
csdn bug11:待加
Explanation of Z-index attribute
Android quick shutdown app
C++ exception implementation mechanism
Incomplete Polyfill of proxy
On fedlearner, the latest open source federated machine learning platform of byte
Coding style: SSM environment in MVC mode, code hierarchical management
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
[leetcode] 93 balanced binary tree
Solution of MAC terminal iterm2 supporting RZ and sz
商品管统——采购需求合并到采购单
编码风格:Mvc模式下SSM环境,代码分层管理
使用call、apply和bind解决js中烦人的this,事件绑定时的this和传参问题
CUDA_ Get the specified device
树莓派鼓捣记 - 设置 wifi
一个名为不安全的类Unsafe
Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop