search_rotate_array
search_rotate_array.cpp 源码
//
// Created by roseduan on 2022/2/9.
//
// 搜索旋转排序数组
#include "vector"
using namespace std;
class Solution {
public:
int search(vector<int>& nums, int target) {
int low = 0, high = nums.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[low] <= nums[mid]) {
if (nums[low] <= target && nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if (nums[high] >= target && target > nums[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
};
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦