-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
63 lines (62 loc) · 1.63 KB
/
Solution.java
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
public class Solution {
public int search(int[] nums, int target) {
if (nums.length==1) {
if (nums[0]==target) {
return 0;
}else {
return -1;
}
}
int begin=0;
int end=nums.length-1;
int half=(end+begin)/2;
while (end-begin>1) {
int b=nums[begin];
int h=nums[half];
int e=nums[end];
if (h==target) {
return half;
}
if (b==target) {
return begin;
}
if (e==target) {
return end;
}
if (h>b) {
if (target>h) {
begin=half;
half=(end+begin)/2;
}else {
if (target>b) {
end=half;
half=(end+begin)/2;
}else {
begin=half;
half=(end+begin)/2;
}
}
}else {
if (target<h) {
end=half;
half=(end+begin)/2;
}else {
if (target>e) {
end=half;
half=(end+begin)/2;
}else {
begin=half;
half=(end+begin)/2;
}
}
}
}
if (nums[begin]==target) {
return begin;
}
if (nums[end]==target) {
return end;
}
return -1;
}
}