Illustration of LeetCode brush problem plan
1. Write it first
Hand-drawn comic book series officially launched!!” LeetCode brush plan “here it comes!!
Today is the 15th issue, strive for a daily issue, at most two days, welcome everyone to supervise me…
Start writing code in C++
Template summary:
- 【 freehand caricature 】 Two parts of the interview to find (solution template and in-depth analysis), last time
- No longer afraid of girlfriend to ask me dichotomy search! Binary Search (Revised edition) (LeetCode 704)
- No longer afraid of girlfriend to ask me dichotomy search!! 【 freehand cartoon 】 The interview must test two search (solution template and in-depth analysis), the final answer
2, the topic
So let’s look at the problem first,
target
3, the body
Can be found:
4, code,
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty()||nums.back()<target) return nums.size();
int left=0;
int right=nums.size()- 1;
while(left<right){
int mid=left+right>>1;
if(nums[mid]>=target){
right=mid;
}
else{
left=mid+1; }}returnleft; }};Copy the code
If lucky enough to help you, please help me a [praise], to a [attention]! I would appreciate an encouragement along the way.
If you want more resources, please follow @I’m Guan Xiaoliang, MAX~