-
Notifications
You must be signed in to change notification settings - Fork 21
/
Search for a Range.java
executable file
·186 lines (152 loc) · 4.25 KB
/
Search for a Range.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
M
1528173258
tags: Array, Binary Search
给sorted array, 有重复数字, 找跟target重合所在的range.
#### Binary Search
- 2个while loop
- 找first/last occurance
- TODO: Can the code be simplified?
```
/*
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
Tags Expand
Binary Search Array Sorted Array
*/
/*
Recap: 12.08.2015
input sorted
2 binary search while loop.
First one, keep looking for 1st occurnace.
Second one, keep looking for alst occurance.
check border case:
A = []
*/
public class Solution {
public int[] searchRange(int[] A, int target) {
int[] rst = new int[]{-1, -1};
if (A == null || A.length == 0) {
return rst;
}
int start = 0;
int end = A.length - 1;
int mid = start + (end - start)/2;
//1st occurance.
int first = 0;
while (start + 1 < end) {
mid = start + (end - start)/2;
if (A[mid] == target) {
if (mid - 1 >= 0 && A[mid - 1] == target) {
end = mid;
continue;
}
break;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (A[start] == target) {
first = start;
} else if (A[mid] == target) {
first = mid;
} else if (A[end] == target) {
first = end;
} else {
return rst;
}
//check last occurance
int last = first;
start = first;
end = A.length - 1;
while (start + 1 < end) {
mid = start + (end - start)/2;
if (A[mid] == target) {
if (mid + 1 < A.length && A[mid + 1] == target) {
start = mid;
continue;
}
break;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (A[end] == target) {
last = end;
} else if (A[mid] == target) {
last = mid;
} else if (A[start] == target) {
last = start;
}
rst[0] = first;
rst[1] = last;
return rst;
}
}
//Older solution:
public class Solution {
/**
*@param A : an integer sorted array
*@param target : an integer to be inserted
*return : a list of length 2, [index1, index2]
*/
public ArrayList<Integer> searchRange(ArrayList<Integer> A, int target) {
// write your code here
int start = 0;
int end = A.size() - 1;
int mid;
ArrayList<Integer> bound = new ArrayList<Integer>();
bound.add(-1);
bound.add(-1);
if ( A.size() == 0) {
return bound;
}
//Left:
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (A.get(mid) < target) {
start = mid;
} else if (A.get(mid) > target) {
end = mid;
} else {
end = mid;//Run to left
}
}
if (A.get(start) == target) {//Run to left
bound.set(0, start);
} else if (A.get(end) == target) {
bound.set(0, end);
} else {
return bound;
}
//Right:
start = 0;
end = A.size() - 1;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (A.get(mid) < target) {
start = mid;
} else if (A.get(mid) > target) {
end = mid;
} else {
start = mid;//Run to right
}
}
if (A.get(end) == target) {//Run to right
bound.set(1, end);
} else if (A.get(start) == target) {
bound.set(1, start);
} else {
return bound;
}
return bound;
}
}
```