-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRotatedSortedArray.java
More file actions
43 lines (41 loc) · 1.05 KB
/
Copy pathSearchInRotatedSortedArray.java
File metadata and controls
43 lines (41 loc) · 1.05 KB
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
package Array;
public class SearchInRotatedSortedArray
{
public int search(int[] A ,int target)
{
int begin = 0;
int end = A.length;//end初值是数组的个数
int middle = 0;
while(begin != end )
{
middle = (begin + end)/2;
if (A[middle] == target)
{
return middle;
}
if (A[begin] <= A[middle])
{
if (A[begin] <= target && target < A[middle])//左边加等号是因为target可能等于A[begin]
{
end = middle;//这里不要写成middle-1,否则会找不到开始和最后的值
}else {
begin = middle + 1;
}
}else {
if (A[middle] < target && target <= A[end-1])//右边加等号是因为target可能等于A[end-1]
{
begin = middle + 1;
}else {
end = middle;//这里不要写成middle-1,否则会找不到开始和最后的值
}
}
}
return -1;
}
public static void main(String[] args)
{
SearchInRotatedSortedArray searchInRotatedSortedArray = new SearchInRotatedSortedArray();
int a[] = {1,2,3};
System.out.println(searchInRotatedSortedArray.search(a, 1));
}
}