Skip to content

Commit 6a2d1a7

Browse files
author
Li Li
committed
add code of 35
1 parent 340c74c commit 6a2d1a7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)