- Topic:
A sorted array of the given integers, finding the starting and ending positions of the given target values. The run-time complexity of your algorithm must be the Order of O (log n). If the target is not found in the array, then [-1,-1] is returned. For example, given [5,7,7,8,8,ten] and the target value 8,return [3,4].
- Idea: This topic directly uses the container to store the location where the find element is found. Remove the first and last one.
- Code
classSolution { Public: Vector<int> Searchrange (intA[],intNinttarget) {Vector<int>Res; Vector<int>v; intFirst =0, last =0; for(intI=0; I<n;i + +){ if(target = =A[i]) {Res.push_back (i); } } if(res.size () = =0) { First= -1; Last= -1; }Else if(res.size () = =1) { First= res[0]; Last= res[0]; } Else{ First=Res.front (); Last= Res[res.size ()-1]; } v.push_back (first); V.push_back (last); returnv; }};
(find) Find the specified worthy start and end position in the array