0%

35. 搜索插入位置

35. 搜索插入位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left=0;
int right=nums.size()-1;
int middle;
while(left<=right){
middle=(left+right)/2;
if (nums[middle]>target){
right=middle-1;
}
else if (nums[middle]<target){
left=middle+1;
}
else{
return middle;
}
}
return right+1;

}
};
-------------本文结束感谢您的阅读-------------
老板你好,讨口饭吃