当前位置:网站首页>Leetcode skimming ---704
Leetcode skimming ---704
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given a n The elements are ordered ( Ascending ) integer array nums And a target value target , Write a function search nums Medium target, If the target value has a return subscript , Otherwise return to -1.
Input :
nums= [-1,0,3,5,9,12], target = 9
Output :4
Method 1 : Two points search
class Solution {
public:
int search(vector<int>& nums, int target) {
int low = 0, high = nums.size() - 1;
while (low <= high) {
int mid = (high - low) / 2 + low;
int num = nums[mid];
if (num == target) {
return mid;
} else if (num > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
};Complexity analysis
Time complexity :O(logn)
Spatial complexity :O(1)
边栏推荐
- Leetcode刷题---832
- 神经网络入门之模型选择(PyTorch)
- Hands on deep learning pytorch version exercise solution - 3.1 linear regression
- Leetcode刷题---704
- Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
- SQL Server Management Studio cannot be opened
- Model evaluation and selection
- Leetcode刷题---374
- Timo background management system
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
猜你喜欢

Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
![[LZY learning notes -dive into deep learning] math preparation 2.5-2.7](/img/57/579357f1a07dbe179f355c4a80ae27.jpg)
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7

I really want to be a girl. The first step of programming is to wear women's clothes

Label Semantic Aware Pre-training for Few-shot Text Classification

Leetcode-106: construct a binary tree according to the sequence of middle and later traversal

Preliminary knowledge of Neural Network Introduction (pytorch)

Ut2014 supplementary learning notes

Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression

神经网络入门之矩阵计算(Pytorch)

Leetcode-112: path sum
随机推荐
Ind yff first week
Deep Reinforcement learning with PyTorch
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
波士顿房价预测(TensorFlow2.9实践)
Julia1.0
A complete mall system
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
Model selection for neural network introduction (pytorch)
20220609 other: most elements
深度学习入门之线性回归(PyTorch)
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
Leetcode刷题---263
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
High imitation bosom friend manke comic app
Tensorflow—Image segmentation
[LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
C#项目-寝室管理系统(1)
Ut2014 learning notes
Stroke prediction: Bayesian
Ut2017 learning notes