One of the basic problems is to mingle in the foundation of a variety of problems, sometimes in small companies to see the big questions, but more is seen in the choice of questions ...
Test instructions: In an ordered series, insert a number of target to find the insertion position.
Landlord here to update the "two-point search review" the first problem of the solution, more similar, of course, is temporarily written today.
Know this problem completed the second Leetcode 4 of the two-point notation, landlord lazy ...
1 classSolution {2 Public:3 intSearchinsert (vector<int>& Nums,inttarget) {4 intL =0, r = nums.size ()-1;5 while(L <=R) {6 intMID = L + (r-l)/2;7 if(Nums[mid] <target) {8L = mid +1;9 }Ten ElseR = Mid-1; One } A returnl; - } -};
Here gives the landlord at that time a problem solution, please do not despise landlord ...
1 class solution { 2 public : 3 int >& nums, int target) { 4 vector< Lower_bound (Nums.begin (), Nums.end (), target); 5 Nums.begin (); 6 7 };
Yes, again the library function ^_^
Leetcode Search Insert Position binary search (two sub-subscript)