#LintCode# Binary Search

既然是菜鸟, 就从基础题开始做起吧, 争取做一题有一题的收获.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.

Challenge  Expand 

If the count of numbers is bigger than MAXINT, can your code work properly?

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

刚开始做的时候没有看清题目要求, 按照普通binarySearch写了一通, 写完才发现题目中是有可能有重复元素的, 要返回最小的index...于是写了下面这个ugly版本的代码:
class Solution {
	/**
	 * @param array
	 *            source array
	 * @param target
	 *            target to search
	 * @return the first occurrence position of target
	 */
	public static int binarySearch(int[] array, int target) {
		// write your code here
		if (array == null) {
			return -1;
		}
		int start = 0;
		int end = array.length - 1;
		int result = Integer.MAX_VALUE;
		int mid;
		while (start < end - 1) {
			mid = start + (end - start) / 2;
			if (array[mid] == target) {
				if (mid < result) {
					result = mid;
					end = mid;
					continue;
				}
			}
			if (array[mid] < target) {
				start = mid;
				continue;
			}
			if (array[mid] > target) {
				end = mid;
				continue;
			}

		}
		if (result != Integer.MAX_VALUE) {
			return result;
		} else {
			return -1;
		}
	}
}
比较原生态, 而且也犯了一些小错误.. 参考了九章的答案, 更改如下:
package BinarySearch;

import java.util.*;

/**
 * Easy Binary Search Show Result My Submissions
 * 
 * 13% Accepted 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.
 * 
 * @author JohnnyHuo
 * 
 */
class Solution {
	/**
	 * @param array
	 *            source array
	 * @param target
	 *            target to search
	 * @return the first occurrence position of target
	 */
	public static int binarySearch(int[] array, int target) {
		// write your code here
		if (array == null) {
			return -1;
		}
		int start = 0;
		int end = array.length - 1;
		int result = Integer.MAX_VALUE;
		int mid;
		while (start < end - 1) {
			mid = start + (end - start) / 2;
			if (array[mid] == target) {
				// if (mid < result) {
				// result = mid;
				end = mid;
				continue;
				// }
			}
			if (array[mid] < target) {
				start = mid;
				continue;
			}
			if (array[mid] > target) {
				end = mid;
				continue;
			}

		}
		// if (result != Integer.MAX_VALUE) {
		// return result;
		// } else {
		// return -1;
		// }
		if (array[start] == target) {
			return start;
		}

		if (array[end] == target) {
			return end;
		}

		return -1;

	}

	public static void main(String[] args) {
		// List list = new ArrayList();
		// list.add(1);
		// list.add(2);
		// list.add(3);
		// list.add(3);
		// list.add(4);
		// list.add(5);
		// list.add(10);

		int[] arr = { 1, 2, 3, 3, 4, 5, 10 };

		System.out.println(binarySearch(arr, 3));
		System.out.println(binarySearch(arr, 11));

	}
}

写的test居然错误的用成了List.... 基础太差, 多练习吧!

总结一下 binary search 要注意的地方:


1. 循环结束的判定条件: 用start < end - 1, 而不用  while(start <= end), 这样可以避免特请情况下的死循环, 比如当 start = 1, end = 2, 那么 mid  = 1 + (2 - 1)/2 = 1. 如果下一步令 start = mid, 则一直死循环了

2. mid的赋值用 mid = start + (end - start) / 2, 而不用 mid = (start + end)/2, 如此一来可以避免因为 start和end都比较大, 相加大于 Integer.MAX_VALUE时的整数溢出

3. 参考的代码里巧妙的用了 if(target == array[mid]) {end = mid},  这样就让代码在已经检测到target的情况下继续往index小的方向查询.



 
 

你可能感兴趣的:(LintCOde,binarySearch,lintcode)