MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0035/Solution.java:11–30  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/05/02 desc :

Source from the content-addressed store, hash-verified

9 * </pre>
10 */
11public class Solution {
12 public int searchInsert(int[] nums, int target) {
13 int left = 0, right = nums.length - 1, mid = (right + left) >> 1;
14 while (left <= right) {
15 if (target <= nums[mid]) right = mid - 1;
16 else left = mid + 1;
17 mid = (right + left) >> 1;
18 }
19 return left;
20 }
21
22 public static void main(String[] args) {
23 Solution solution = new Solution();
24 int[] nums = new int[]{1, 3, 5, 6};
25 System.out.println(solution.searchInsert(nums, 5));
26 System.out.println(solution.searchInsert(nums, 2));
27 System.out.println(solution.searchInsert(nums, 7));
28 System.out.println(solution.searchInsert(nums, 0));
29 }
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected