forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchInABigSortedArray.java
68 lines (60 loc) · 1.9 KB
/
searchInABigSortedArray.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
// Source : http://www.lintcode.com/en/problem/search-in-a-big-sorted-array/
// Inspired by : http://www.jiuzhang.com/solutions/search-in-a-big-sorted-array/
// Author : Lei Cao
// Date : 2015-10-05
/**********************************************************************************
*
* Given a big sorted array, find the first index of a target number. Your algorithm should be in O(log k), where k is the first index of the target number.
*
* Return -1, if the number doesn't exist in the array.
*
* Example
* Given [1, 3, 6, 9, 21], and target = 3, return 1.
*
* Challenge
* O(log k), k is the first index of the given target number.
*
**********************************************************************************/
package searchInABigSortedArray;
/**
* Created by leicao on 5/10/15.
*/
public class searchInABigSortedArray {
public int searchBigSortedArray(int[] A, int target) {
if (A == null || A.length == 0) {
return -1;
}
int start = 0;
int end = A.length - 1;
int upperBound = 1;
// @TODO The logic here needs to be reviewed.
for (int i = 0; i < Math.sqrt(A.length) + 1; i++) {
if (upperBound < 0) {
end = A.length - 1;
break;
}
if (A.length > upperBound && A[upperBound] > target) {
end = upperBound - 1;
break;
}
upperBound = upperBound * 2;
}
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (A[mid] == target) {
end = mid;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (A[start] == target) {
return start;
}
if (A[end] == target) {
return end;
}
return -1;
}
}