当前位置:网站首页>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 bug9: to be added
- Explanation of Z-index attribute
- CSDN bug8: to be added
- [leetcode] 93 balanced binary tree
- “wget: 无法解析主机地址”的解决方法
- Youtube订阅——解决在弹窗内使用Youtube订阅按钮高度显示不全的问题
- Hong Kong listed companies transfer cards to acquire 42.5% equity of chuangxinzhong and plan to speed up the distribution of marketing services
- If you need a million objects
- [elixir! 0073] beam built-in memory database ETS
- For programmers, those unfamiliar and familiar computer hardware
猜你喜欢

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

极验无感验证破解

Youtube订阅——解决在弹窗内使用Youtube订阅按钮高度显示不全的问题

接缝雕刻算法:一种看似不可能的图像大小调整方法

【技术教程】Visual Studio 2017自建WebRTC中peerconnection_client程序报LNK2019 无法解析的外部符号错误

【LeetCode】 93 平衡二叉树

CSDN bug7: to be added

mac终端Iterm2支持rz和sz的解决方案

CSDN bug11: to be added

Learning from scratch YoMo series: Opening
随机推荐
Filezilla server配置FTP服务器中的各种问题与解决方法
An unsafe class named unsafe
JMeter interface test -- a solution with token
C++异常实现机制
[论文阅读笔记] Large-Scale Heterogeneous Feature Embedding
完美日记母公司逸仙电商招股书:重营销、轻研发,前三季度亏11亿
“wget: 无法解析主机地址”的解决方法
CSDN bug10: to be added
Hong Kong listed companies transfer cards to acquire 42.5% equity of chuangxinzhong and plan to speed up the distribution of marketing services
异常:Invalid or unexpected token
编码风格:Mvc模式下SSM环境,代码分层管理
CUDA_主机内存
港股上市公司移卡收购创信众42.5%股权 谋划加快营销服务布局
一个名为不安全的类Unsafe
Raspberry pie drum set WiFi
OSChina 周二乱弹 —— 我养的绿植分别为土豆,生姜,蒜
[leetcode] 92 integer inversion
C++ exception implementation mechanism
CSDN bug4: to be added
CSDN bug6: to be added