Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3df899

Browse files
committed
Time: 0 ms (100.00%), Space: 41.9 MB (50.67%) - LeetHub
1 parent 5978f8d commit f3df899

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public int searchInsert(int[] nums, int target) {
3+
4+
int low = 0;
5+
int high = nums.length - 1;
6+
7+
if (target <= nums[low]) {
8+
return 0;
9+
}
10+
11+
if (target == nums[high]) {
12+
return high;
13+
}
14+
15+
if (target > nums[high]) {
16+
return high + 1;
17+
}
18+
19+
while (low <= high) {
20+
int mid = (high + low) / 2;
21+
if (target == nums[mid]) {
22+
return mid;
23+
}
24+
if (mid - 1 >= 0 && target > nums[mid - 1] && target < nums[mid]) {
25+
return mid;
26+
}
27+
if (target > nums[mid]) {
28+
low = mid + 1;
29+
} else {
30+
high = mid - 1;
31+
}
32+
}
33+
return low;
34+
}
35+
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /