当前位置:网站首页>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刷题---75
- Hands on deep learning pytorch version exercise solution - 3.1 linear regression
- Leetcode刷题---278
- Leetcode刷题---189
- Tensorflow—Image segmentation
- Leetcode刷题---832
- Ut2011 learning notes
- Knowledge map enhancement recommendation based on joint non sampling learning
- Data preprocessing - Data Mining 1
- Jetson TX2 刷机
猜你喜欢
Leetcode - 705 design hash set (Design)
Boston house price forecast (tensorflow2.9 practice)
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
What can I do to exit the current operation and confirm it twice?
A complete mall system
七、MySQL之数据定义语言(二)
Timo background management system
Data classification: support vector machine
Model evaluation and selection
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
随机推荐
Ut2013 learning notes
安装yolov3(Anaconda)
Leetcode刷题---217
Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
Softmax regression (pytorch)
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
Leetcode-513: find the lower left corner value of the tree
Leetcode刷题---202
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
A super cool background permission management system
Leetcode刷题---852
八、MySQL之事务控制语言
Ut2016 learning notes
六、MySQL之数据定义语言(一)
Leetcode刷题---977
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
二分查找法
20220601 Mathematics: zero after factorial
Linear regression of introduction to deep learning (pytorch)
Boston house price forecast (tensorflow2.9 practice)