Skip to content

Commit 1de645f

Browse files
authoredJan 23, 2022
Add files via upload
1 parent a9c4b8e commit 1de645f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
 

‎搜索插入位置_35.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class 搜索插入位置_35 {
2+
public int searchInsert(int[] nums, int target) {
3+
int left = 0, right = nums.length - 1;
4+
while(left <= right) {
5+
int mid = left + (right - left)/2; // 比(left + right) /2更安全
6+
if(nums[mid] == target) {
7+
return mid;
8+
} else if(nums[mid] < target) {
9+
left = mid + 1;
10+
} else {
11+
right = mid - 1;
12+
}
13+
}
14+
return left;
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.