当前位置:网站首页>Leetcode 1-sum of two numbers
Leetcode 1-sum of two numbers
2020-11-10 08:39:00 【open_nfiwhlc1】
List of articles
Title Description
Given an array of integers and a target value , Find the two numbers in the array that are the target values .
You can assume that each input corresponds to only one answer , And the same elements cannot be reused .
Example :
Given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9
So back [0, 1]
Method 1
Violence enumeration
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;
}
};
Ideas and algorithms
The easiest way to think of is to enumerate every number in an array x, Look for the existence of target - x.
When we look for target - x when , It needs to be noted that each is located in x All the previous elements have been associated with x Matched , So there's no need to match again . And each element cannot be used twice , So we just need to x Look for... In the following elements target - x.
Complexity analysis
- Time complexity :O(N^2)O(N2), among NN Is the number of elements in the array . In the worst case, any two numbers in the array must be matched once .
- Spatial complexity :O(1)O(1).
Method 2
Hashtable
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 {};
}
};
Ideas and algorithms
Note that the reason for the high time complexity of method 1 is to find target - x The time complexity of is too high . therefore , We need a better approach , Can quickly find whether the target element exists in the array . If there is , We need to find out its index .
Use hash table , You can look for target - x The time complexity is reduced to from O(N)O(N) Down to O(1)O(1).
So we create a hash table , For each of these x, We first query the hash table for the presence of target - x, And then x Insert into hash table , You can guarantee that you won't let x Match yourself .
Complexity analysis
- Time complexity :O(N)O(N), among NN Is the number of elements in the array . For each element x, We can O(1)O(1) Looking for target - x.
- Spatial complexity :O(N)O(N), among NN Is the number of elements in the array . Mainly for the cost of hash table .
版权声明
本文为[open_nfiwhlc1]所创,转载请带上原文链接,感谢
边栏推荐
- pytorch训练GAN时的detach()
- 【技术教程】Visual Studio 2017自建WebRTC中peerconnection_client程序报LNK2019 无法解析的外部符号错误
- Filezilla server配置FTP服务器中的各种问题与解决方法
- Fire knowledge online answer activity small program
- 大专学历的我工作六年了,还有机会进大厂吗?
- 解决Coursera视频无法观看的三种方法(亲测有效)
- Problems and solutions in configuring FTP server with FileZilla server
- CCR炒币机器人:新冠肺炎加速了监管机构对CBDC的兴趣
- iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本
- 一个名为不安全的类Unsafe
猜你喜欢
港股上市公司移卡收购创信众42.5%股权 谋划加快营销服务布局
YouTube subscription: solve the problem of incomplete height display of YouTube subscription button in pop-up window
完美日记母公司逸仙电商招股书:重营销、轻研发,前三季度亏11亿
极验无感验证破解
Oschina: my green plants are potatoes, ginger and garlic
Seam engraving algorithm: a seemingly impossible image size adjustment method
csdn bug11:待加
仅发送options请求,没有发送post解决方案
C++ STL容器篇
Problems and solutions in configuring FTP server with FileZilla server
随机推荐
After seven years of pursuing, nearly one billion US dollars of bitcoin was eventually confiscated and confiscated by the US government
Factory approach model
分布式文档存储数据库之MongoDB索引管理
iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本
Coding style: SSM environment in MVC mode, code hierarchical management
[elixir! #0073] beam 内置的内存数据库 —— ETS
If you need a million objects
csdn bug4:待加
leetcode之最后一个单词的长度
CCR炒币机器人:新冠肺炎加速了监管机构对CBDC的兴趣
Fire knowledge online answer activity small program
jt-day10
The length of the last word in leetcode
安卓快速关机APP
Coding style: SSM environment in MVC mode, code hierarchical management
csdn bug7:待加
编码风格:Mvc模式下SSM环境,代码分层管理
OSChina 周二乱弹 —— 我养的绿植分别为土豆,生姜,蒜
csdn bug9:待加
网络安全工程师演示:原来***是这样控制你的服务器的!(下)