We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 340c74c commit 6a2d1a7Copy full SHA for 6a2d1a7
Algorithms Basics/Chapter 6. Binary Search/35. Search Insert Position.cs
@@ -0,0 +1,20 @@
1
+public class Solution {
2
+ public int SearchInsert(int[] nums, int target) {
3
+ if (nums == null || nums.Length == 0) return 0;
4
+ int start = 0;
5
+ int end = nums.Length - 1;
6
+ while (start + 1 < end) {
7
+ int mid = start + (end - start) / 2;
8
+ if (nums[mid] == target) {
9
+ return mid;
10
+ } else if (nums[mid] < target) {
11
+ start = mid;
12
+ } else {
13
+ end = mid;
14
+ }
15
16
+ if (target <= nums[start]) return start;
17
+ if (nums[start] < target && target <= nums[end]) return end;
18
+ return end+1;
19
20
+}
0 commit comments