-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInRotatedArray.java
106 lines (97 loc) · 2.22 KB
/
SearchInRotatedArray.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ProjectFiles;
import java.util.Arrays;
public class SearchInRotatedArray {
public static void main(String[] args) {
System.out.println(search(new int[]{2} , 2));
System.out.println(search(new int[]{6,7,7,7,1,1,1,2} , 6));
}
private static int search1(int[] nums, int target) {
if(nums == null || nums.length == 0){
return -1;
}
int left = 0;
int right = nums.length -1;
int result = 0;
while(right - left >= 2){
if(nums[right] >= nums[left]){
result = Arrays.binarySearch(nums, left, right+1, target);
if( result >= 0){
return result;
}
else
return -1;
}
else{
int mid = (left + right )/2;
if(target == nums[mid])
return mid;
if(target == nums[left])
return left;
if(target == nums[right])
return right;
if((target > nums[mid] && target > nums[right]) || (target < nums[mid] && target < nums[right]) ){
left = mid+1;
}
else if((target < nums[mid] && target > nums[right]) || (target > nums[mid] && target < nums[right]) ){
right = mid-1;
}
}
}
if(target == nums[left])
return left;
if(target == nums[right])
return right;
return -1;
}
private static int search(int[] nums, int target) {
if(nums == null || nums.length == 0)
return -1;
int left = 0;
int right = nums.length -1;
int mid ;
int midValue ;
int result;
while(right >= left){
if(nums[right] >= nums[left]){
result = Arrays.binarySearch(nums, left, right+1, target);
if( result >= 0){
return result;
}
else
return -1;
}
else{//nums[right] < nums[left] rotated
mid = (left+right)/2;
midValue = nums[mid];
if(target == midValue)
return mid;
if(midValue > nums[right]){
if(target > midValue){
left = mid + 1;
}
else {//target <= midValue
if( target > nums[right]){
right = mid -1;
}
else if( target <= nums[right]){
left = mid + 1;}
}
}
else{//midValue <= nums[right]
if(target < midValue){
right = mid -1;
}
else{//target >= midValue)
if(target > nums[right]){
right = mid -1;
}
else{//target <= nums[right]
left = mid +1;
}
}
}
}
}
return -1;
}
}