Inscription Leetcode brush (11) - Search insertion position

Topics requirements:

Given a sorted array and a target, find the object in the array, and returns its index. If the target is not present in the array, it will be returned in sequence inserted position.

Solution to a problem : violence to solve && binary search

The simplest idea is to solve the violence, a cycle judging output.

Given the array is sorted, it is madness to suggest we use "dichotomy"! ! !

Median definition dichotomy mid: direct use mid = (left + right) / 2 prone to overflow problems, unsigned right shift method: mid = (left + right) >>> 1;

Thinking this question is very clear:

(1) determines whether a target value greater than the last element in the array, if the length of the array is direct output; otherwise, turn 2

(2) using the binary search method, to find half a time element, and returns the index value left.

 

Guess you like

Origin www.cnblogs.com/wangjm63/p/11497598.html