-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_10.03_search.cc
69 lines (63 loc) · 1.41 KB
/
Problem_10.03_search.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <vector>
using namespace std;
// 旋转数组
// 此题是第 81 题的进阶,存在相同的值,且查找最左边的
class Solution
{
public:
// 思路 - 二分查找
// 1、先判断数组左半数组还是右半数组升序
// 2、然后再进行二分查找
// 3、最后对重复值或结果进行判断
// TODO: figure it out.
int search(vector<int>& arr, int target)
{
int left = 0;
int right = arr.size() - 1;
while (left < right)
{
int mid = left + (right - left) / 2; // 防止 left + right 过大
// 左半数组升序
if (arr[left] < arr[mid])
{
if (arr[left] <= target && target <= arr[mid])
{
// 目标在左半数组
right = mid;
}
else
{
// 目标在右半数组
left = mid + 1;
}
}
// 右半数组升序
else if (arr[left] > arr[mid])
{
if (arr[right] > target && target > arr[mid])
{
// 目标在右半数组
left = mid + 1;
}
else
{
// 目标在左半数组
right = mid;
}
}
// 可能找到目标,也可能遇到重复值
else
{
if (arr[left] != target)
{
left++;
}
else
{
right = left;
}
}
}
return (arr[left] == target) ? left : -1;
}
};