Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int BinarySearch(int[] A, int target)
int begin = 0;
int end = A.length ;
while(begin < end) {
int p = (end + begin) / 2;
if(A[p] == target) return p;
else if(A[p] < target) {
begin = p + 1;
}
else {
end = p;
}
}
return -1;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int BinarySearch(int[] A, int target)
int begin = 0;
int end = A.length-1;
while(begin <= end) {
int p = (end + begin) / 2;
if(A[p] == target) return p;
else if(A[p] < target) {
begin = p + 1;
}
else {
end = p - 1;
}
}
return -1;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
There are no major differences between each other. The only thing you need to remember is the condition in the while loop
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Instead of giving -1 when search fails, give the index that the number could be inserted and maintain the order
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Example: 1,2,4,6,7,8,9
input 4 return 2
input 5 return 3 (Previously is -1)
public int BinarySearch(int[] A, int target)
int begin = 0;
int end = A.length ;
while(begin < end) {
int p = (end + begin) / 2;
if(A[p] == target) return p;
else if(A[p] < target) {
begin = p + 1;
}
else {
end = p;
}
}
return begin;
}
Find first occurrence of a target in a sorted array
idx | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
Array[] | 6 | 7 | 7 | 7 | 7 | 7 |
Find last occurrence of a target in a sorted array
first iteration: L = 0, R = 6, M = 3 => R = M = 3
second iteration: L = 0, R = 3, M = 1 => R = M = 1
L is neighbor of R, break;
Post processing
int binarySearch(vector<int>& a, int target){
int left = 0, right = a.size() - 1,
int mid;
while(left < right - 1){
int mid = left + (right - left)/2;
if(a[mid] == target){
left = mid;
}
else if(a[mid] < target){
left = mid;
}
else {
right = mid;
}
}
if(left == target) return left;
if(right == target) return right;
return -1;
}
int binarySearch(vector<int>& a, int target){
int left = 0, right = a.size() - 1,
int mid;
while(left < right - 1){
int mid = left + (right - left)/2;
if(a[mid] == target){
right = mid;
}
else if(a[mid] < target){
left = mid;
}
else {
right = mid;
}
}
if(right == target) return right;
if(left == target) return left;
return -1;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Example: 6 7 8 9 1 2 3 4 5
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int search(int[] A, int target) {
int begin = 0;
int end = A.length;
while(begin < end) {
int p = (end + begin) / 2;
if(A[p] == target) return p;
else if(A[p] > A[begin]){
if(target >= A[begin] && target < A[p]) {
end = p;
}
else begin = p + 1;
}
else {
if(target > A[p] && target <= A[end - 1] ) {
begin = p + 1;
}
else end = p;
}
}
return -1;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public boolean search(int[] nums, int target) {
int begin = 0;
int end = nums.length;
while(begin < end) {
int p = (begin + end) / 2;
if(nums[p] == target) return true;
if(nums[p] > nums[begin]) {
if(target >= nums[begin] && target < nums[p]) {
end = p;
}
else begin = p + 1;
}
else if(nums[p] < nums[begin]) {
if(target > nums[p] && target <= nums[end - 1] ) {
begin = p + 1;
}
else end = p;
}
else {
begin ++;
}
}
return false;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int mySqrt(int x) {
if(x <= 1) return x;
Long begin = (long)0;
Long end = (long)x / 2 + 1;
while(begin < end) {
Long p = (end + begin) / 2;
if(p * p == x) return p.intValue();
else if(p * p < x) {
begin = p + 1;
}
else {
end = p;
}
}
return end.intValue() - 1;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/