-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60.h
42 lines (39 loc) · 1.05 KB
/
60.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
/*
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: An integer
*/
int searchInsert(vector<int> &A, int target) {
// write your code here
if(A.size()==0) return 0;
//verson 1
// int l = 0, r = A.size();
// while(l < r){
// int mid = l + (r-l)/2;
// if(target <= A[mid]) r = mid ;
// else l = mid + 1;
// }
// return r;
//verson2
// int left = 0, right = A.size();
// while (left < right) {
// int mid = left + (right - left) / 2;
// if (A[mid] == target) return mid;
// else if (A[mid] < target) left = mid + 1;
// else right = mid;
// }
// return right;
//version3 by wangpei
int low=0, high=A.size()-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(target==A[mid]) return mid;
else if(target<A[mid]) high=mid-1;
else low=mid+1;
}
return low;
}
};