MCPcopy Create free account
hub / github.com/MisterBooo/LeetCodeAnimation / Solution2

Class Solution2

problems/0035-search-insert-position/Code/2.java:3–21  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//时间复杂度:O(lon(n))
2//空间复杂度:O(1)
3class Solution2 {
4 public int searchInsert(int[] nums, int target) {
5 if (target>nums[nums.length-1]) {
6 return nums.length;
7 }
8 int left=0;
9 int right=nums.length-1;
10 while (left < right) {
11 int mid = (left + right) / 2;
12 if (nums[mid] < target) {
13 left = mid + 1;
14 } else {
15 right = mid;
16 }
17 }
18 return left;
19
20 }
21}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected