当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

csdn bug8:待加

完美日记母公司逸仙电商招股书:重营销、轻研发,前三季度亏11亿
![[paper reading notes] community oriented attributed network embedding](/img/17/1d1989945d943ca3cd2a2b8a5731de.jpg)
[paper reading notes] community oriented attributed network embedding

推动中国制造升级,汽车装配车间生产流水线3D可视化

Mongodb index management of distributed document storage database

Coding style: SSM environment in MVC mode, code hierarchical management

使用call、apply和bind解决js中烦人的this,事件绑定时的this和传参问题
![[paper reading notes] a multilayered informational random walk for attributed social network embedding](/img/3d/657a60600219ce6cfc514ad1b1bb49.jpg)
[paper reading notes] a multilayered informational random walk for attributed social network embedding
![[python学习手册-笔记]001.python前言](/img/c0/b4d34272d3f845ac717d48c669d974.jpg)
[python学习手册-笔记]001.python前言

selenium webdriver使用click一直失效问题的几种解决方法
随机推荐
js解决浏览器打印自动分页的问题
CUDA_ Get the specified device
Bifrost 位点管理 之 异构中间件实现难点(1)
leetcode之最后一个单词的长度
Connection to XXX could not be established. Broker may not be available
《Python Cookbook 3rd》笔记(2.2):字符串开头或结尾匹配
YouTube subscription: solve the problem of incomplete height display of YouTube subscription button in pop-up window
[paper reading notes] large scale heterogeneous feature embedding
csdn bug10:待加
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
史上最全异常检测算法概述
For programmers, those unfamiliar and familiar computer hardware
[论文阅读笔记] Community-oriented attributed network embedding
Assign the corresponding key and value in the map to the object
Difficulties in heterogeneous middleware implementation of Bifrost site management (1)
Youtube订阅——解决在弹窗内使用Youtube订阅按钮高度显示不全的问题
Raspberry pie drum set WiFi
中央重点布局:未来 5 年,科技自立自强为先,这些行业被点名
ServiceManagerProxy中mRemote变量指的什么?
Python cookbook 3rd note (2.1): using multiple qualifiers to split strings