Skip to content

Commit a3b5eb0

Browse files
committed
SearchInsertPosition35
1 parent a4f187f commit a3b5eb0

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/SearchInsertPosition35.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,24 @@ public int searchInsert2(int[] A, int target) {
3737
}
3838
return low;
3939
}
40+
41+
42+
public int searchInsert3(int[] nums, int target) {
43+
if (nums.length == 0) return 0;
44+
if (target > nums[nums.length-1]) return nums.length;
45+
int l = 0;
46+
int r = nums.length-1;
47+
48+
while (l < r) {
49+
int mid = (r - l) / 2 + l;
50+
if (nums[mid] == target) return mid;
51+
else if (nums[mid] < target) {
52+
l = mid + 1;
53+
} else {
54+
r = mid;
55+
}
56+
}
57+
return l;
58+
}
59+
4060
}

0 commit comments

Comments
 (0)