A simple interview questions: as many as 90% of programmers can not write this algorithm is completely correct. . .

Some time ago, at the forum I see statistics say that 90% of programmers can not write to a simple dichotomy. Dichotomy is not very easy right? Is not that sensational?

In fact, the dichotomy really is not so simple, especially the various variants of dichotomy. The most simple dichotomy, is a key value from a sorted array of look. The following procedures.


/**
 * 二分查找,找到该值在数组中的下标,否则为-1
 */
static int binarySerach(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] == key) { return mid; } else if (array[mid] < key) { left = mid + 1; } else { right = mid - 1; } } return -1; } 

This program, I believe that as long as a qualified programmer should be able to write. Little note that each pointer moves left and right, when necessary on the basis of mid +1 or -1, to prevent infinite loop, the program will be able to run correctly.

But if the conditions change a little bit, you will write it? For example, the data may be repeated in the array, the data required to return matching the minimum (or maximum) index; one step closer to a need to find a first element of the array is greater than the key (the key is greater than the minimum ) under standard element, and so on. These, though, when only a little change, they really want to achieve more carefully. Listed below are to achieve these binary search variants.

1, first find out the key elements of equal


// 查找第一个相等的元素
static int findFirstEqual(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] >= key) { right = mid - 1; } else { left = mid + 1; } } if (left < array.length && array[left] == key) { return left; } return -1; } 

2, find the last of the key elements of equal

// 查找最后一个相等的元素
static int findLastEqual(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] <= key) { left = mid + 1; } else { right = mid - 1; } } if (right >= 0 && array[right] == key) { return right; } return -1; } 

3, finds the first element is greater than or equal to the Key

// 查找第一个等于或者大于key的元素
static int findFirstEqualLarger(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] >= key) { right = mid - 1; } else { left = mid + 1; } } return left; } 

4. Find the first element greater than key

// 查找第一个大于key的元素
static int findFirstLarger(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] > key) { right = mid - 1; } else { left = mid + 1; } } return left; } 

5, to find the last key element is equal to or less than

// 查找最后一个等于或者小于key的元素
static int findLastEqualSmaller(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] > key) { right = mid - 1; } else { left = mid + 1; } } return right; } 

6, to find the last element is less than the key

// 查找最后一个小于key的元素
static int findLastSmaller(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] >= key) { right = mid - 1; } else { left = mid + 1; } } return right; } 

Next, we can make the appropriate test these four variants of the algorithm.

A lot of time, local application binary search is not a direct finding and equal key elements, but the use of various variants of binary search mentioned above, mastered these variants, when you use the binary search retrieved again on It will feel even more handy.

Readers welfare

For in the above article I summed up the Internet company java programmers involved in most of face interview questions and answers made the document architecture and free for everyone to share video material (including Dubbo, Redis, Netty, zookeeper, Spring cloud, distributed, high concurrency architecture technical information), hoping to help review your pre-interview and find a good job, but also saves you the time to search for information online learning.

Obtaining: Add qun group: 956 011 797 Click Join now  to find free access to manage the little sister!

合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

 

 

Guess you like

Origin www.cnblogs.com/Longdingtian/p/10985288.html