Binary Search
来源:互联网 发布:大数据培训ujiuye 编辑:程序博客网 时间:2024/06/06 06:35
Binary search is a famous question in algorithm.
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.
If the target number does not exist in the array, return -1.
Example
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
写了一个binary search 也能 time limit exceeded
import java.util.Arrays;class Solution { /** * @param array source array * @param target target to search * @return the first occurrence position of target */ public int binarySearch(int[] array, int target) { //write your code here if (array != null && array.length != 0) { return search(array, target, 0, array.length - 1); } return -1; } private int search(int[] array, int target, int start, int end) { if ((end - start) <= 1) { if (target == array[start]){ return start; } else if (target == array[end]){ return end; } else { return -1; } } int mid = (start + end) / 2; if (target == array[mid]) { int i; for (i = mid; i >= 0; i--) { if (array[i] != target) { break; } } return i; } else if (target < array[mid]) { return search(array, target, start, mid - 1); } else { return search(array, target, start, mid + 1); } }}
非递归就OK了
import java.util.Arrays;class Solution { /** * @param array source array * @param target target to search * @return the first occurrence position of target */ public int binarySearch(int[] array, int target) { //write your code here if (array != null && array.length != 0) { int start = 0; int end = array.length - 1; int mid; while ((end - start) > 1) { mid = (start + end) / 2; if (target == array[mid]) { for (int i = mid - 1; i >= 0; i--) { if (array[i] != target) { return i + 1; } } } else if (target < array[mid]) { end = mid - 1; } else { start = mid + 1; } } if (target == array[start]){ return start; } else if (target == array[end]){ return end; } } return -1; }}
别人的代码:
这个代码主要的一个不同之处是用了:
mid = start + (end - start) / 2;
而不是 mid = (start +end)/2;
Binary search 的 key point:
1. start +1 < end
2. mid = start + (end - start)/2
3. array[mid] == 如何操作
array[mid] < 如何
array[mid] > 如何
4 考虑:
array[start] = target
array[end] = target
class Solution {public: /** * @param array source array * @param target target to search * @return the first occurrence position of target */ int binarySearch(vector<int> &A, int target) { if (A.size() == 0) { return -1; } int start = 0; int end = A.size() - 1; int mid; while (start + 1 < end) { mid = start + (end - start) / 2; if (A[mid] == target) { end = mid; } else if (A[mid] < target) { start = mid; } else if (A[mid] > target) { end = mid; } } if (A[start] == target) { return start; } if (A[end] == target) { return end; } return -1; }};
实际上直接用 start<=end 就可以,不需要最后的两个if 判断了:
import java.util.Arrays;class Solution { /** * @param array source array * @param target target to search * @return the first occurrence position of target */ public int binarySearch(int[] array, int target) { //write your code here if (array != null && array.length != 0) { int start = 0; int end = array.length - 1; int mid; while (end >= start) { mid = (start + end) / 2; if (target == array[mid]) { for (int i = mid - 1; i >= 0; i--) { if (array[i] != target) { return i + 1; } } } else if (target < array[mid]) { end = mid - 1; } else { start = mid + 1; } } /*if (target == array[start]){ return start; } else if (target == array[end]){ return end; }*/ } return -1; }}
0 0
- Binary search
- Binary Search
- Binary Search
- binary search
- binary search
- Binary Search
- binary search
- binary search
- binary search
- binary search
- binary search
- binary search
- Binary Search
- Binary search
- Binary Search
- Binary Search
- Binary Search
- binary search
- Python高级特性(2):Closures、Decorators和functools
- HDU 1114 Piggy-Bank
- 如何得到下载文件的文件名
- POCO开源库的日志使用示例
- 《Android学习指南》目录
- Binary Search
- 实现一个视图在固定圆圈内移动
- html框架及锚点
- Collections:给集合操作提供了更多的功能和Arraylist使用方法
- 详细的AWR解析报告
- JS面向对象编程---对象
- hadoop配置lzo,外加自己遇到错误的解决方法
- shgdsh
- Java 内部类