lincode14- First Position of Target- easy

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 

If the count of numbers is bigger than 2^32, can your code work properly?

二分法模板。nums[mid] == target时 end = mid。这种题目最后找出来,start指的是 <=target的(=出现在一开始就指到的情况),end指的是 >= target的。所以如果要找最接近的,其实也落在他们上面(更前面的或者更后面的不会比他们好)。

原文地址:https://www.cnblogs.com/jasminemzy/p/7594740.html