当前位置:网站首页>Leetcode skimming ---1
Leetcode skimming ---1
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given an array of integers nums And an integer target value target, Find and in the array as the target value target the Two integers , And return their array subscripts . It can be assumed that each input will correspond to only one answer . however , The same element in the array cannot be repeated in the answer . Answers can be returned in any order .
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
Method 1 : 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 {};
}
};
Complexity analysis
Time complexity :O(N)
Spatial complexity :O(N)
边栏推荐
- 20220608 other: evaluation of inverse Polish expression
- 六、MySQL之数据定义语言(一)
- Ind yff first week
- Data preprocessing - Data Mining 1
- Deep Reinforcement learning with PyTorch
- Jetson TX2 刷机
- Label Semantic Aware Pre-training for Few-shot Text Classification
- 20220605 Mathematics: divide two numbers
- Leetcode刷题---217
- Tensorflow—Image segmentation
猜你喜欢
Are there any other high imitation projects
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
七、MySQL之数据定义语言(二)
Ut2017 learning notes
一步教你溯源【钓鱼邮件】的IP地址
Leetcode - the k-th element in 703 data flow (design priority queue)
Tensorflow - tensorflow Foundation
深度学习入门之自动求导(Pytorch)
Ut2012 learning notes
随机推荐
神经网络入门之模型选择(PyTorch)
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
Tensorflow—Neural Style Transfer
Ind yff first week
Multilayer perceptron (pytorch)
Preliminary knowledge of Neural Network Introduction (pytorch)
20220609 other: most elements
侯捷——STL源码剖析 笔记
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
Leetcode刷题---852
Linear regression of introduction to deep learning (pytorch)
6、 Data definition language of MySQL (1)
High imitation wechat
The imitation of jd.com e-commerce project is coming
Leetcode刷题---75
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
A complete mall system
20220531 Mathematics: Happy numbers
丢弃法Dropout(Pytorch)