当前位置:网站首页>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)
边栏推荐
- [LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
- 20220607 others: sum of two integers
- Leetcode刷题---35
- Inverse code of string (Jilin University postgraduate entrance examination question)
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Flutter 退出当前操作二次确认怎么做才更优雅?
- A super cool background permission management system
- 丢弃法Dropout(Pytorch)
- 神经网络入门之矩阵计算(Pytorch)
- Judging the connectivity of undirected graphs by the method of similar Union and set search
猜你喜欢

深度学习入门之自动求导(Pytorch)

Knowledge map enhancement recommendation based on joint non sampling learning

Model evaluation and selection

Ut2016 learning notes

深度学习入门之线性回归(PyTorch)

LeetCode - 715. Range module (TreeSet)*****

Implementation of "quick start electronic" window dragging

Training effects of different data sets (yolov5)

Tensorflow - tensorflow Foundation

Ut2014 supplementary learning notes
随机推荐
The imitation of jd.com e-commerce project is coming
Leetcode刷题---367
Ind yff first week
QT creator uses OpenCV Pro add
Leetcode刷题---832
Leetcode刷题---189
权重衰退(PyTorch)
Install yolov3 (Anaconda)
Notes - regular expressions
Handwritten digit recognition: CNN alexnet
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
LeetCode - 900. RLE iterator
Step 1: teach you to trace the IP address of [phishing email]
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
神经网络入门之矩阵计算(Pytorch)
Ind FHL first week
Tensorflow - tensorflow Foundation