Leetcode_Find related_c++ version

(1) 35 Search Insertion Positions – Simple

Given a sorted array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, returns the position at which it will be inserted sequentially.

Please use an algorithm with a time complexity of O(log n).
Insert image description here
Use binary search

class Solution {
    
    
public:
//二分查找
    int searchInsert(vector<int>& nums, int target) {
    
    
        int begin = 0;
        int end = nums.size() -1;
        while(begin< end){
    
    
            int mid = (begin + end)/2;
			//如果mid对应值即为目标值,则返回插入位置为mid
            if(nums[mid] == target){
    
    
                return mid ;
            }
            //如果mid对应值小于target,则在后半部分查找
            else if(nums[mid] < target){
    
    
                begin = mid + 1;
            }
            //否则,在前半部分查找
            else end = mid - 1;
        }
        //当begin == end时,只需比较begin处与目标数的大小插入数即可
        if(target > nums[begin]) return begin+1;
        else return begin;
    }
};

Insert image description here

(2) 34 Find the first and last position of an element in a sorted array – Medium

You are given an array of integers nums arranged in non-decreasing order, and a target value target. Please find the starting position and ending position of the given target value in the array.

If the target value target does not exist in the array, [-1, -1] is returned.

You must design and implement an algorithm with time complexity O(log n) to solve this problem.
Insert image description here
Use binary search

class Solution {
    
    
public:
    vector<int> searchRange(vector<int>& nums, int target) {
    
    
    
        int begin = 0;
        int end = nums.size() - 1;
        vector<int> result;
        while(begin<= end){
    
    
        
            int mid = (begin+ end)/2;
            //二分查找,如果mid = target,左右端点向两边扩散
            if(nums[mid] == target){
    
    
            //左端点向左扩散
                begin = mid ;
                while(begin - 1 >= 0 && nums[begin - 1] == target )
                    begin = begin -1;
                //右端点向右扩散
                end = mid;
                while(end + 1 < nums.size() && nums[end + 1] == target)
                    end = end + 1;
                //得到区间范围
                result.push_back(begin);
                result.push_back(end);
                return result;
            }
            else if (nums[mid] <target){
    
    
                begin = mid + 1;
            }
            else end = mid - 1;
        }
        //如果没找到目标元素,返回-1
        result.push_back(-1);
        result.push_back(-1);
        return result;
    }
};

Insert image description here

(3) 33 Search Rotated Sorted Array – Medium

The integer array nums is arranged in ascending order, and the values ​​in the array are distinct from each other.

Before being passed to the function, nums is rotated at some previously unknown subscript k (0 <= k < nums.length), making the array become [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]] (the subscripts start counting from 0). For example, [0,1,2,4,5,6,7] may become [4,5,6,7,0,1,2] when rotated at index 3.

Give you the rotated array nums and an integer target. If the target value target exists in nums, return its subscript, otherwise return -1.

You must design an algorithm with time complexity O(log n) to solve this problem.
Insert image description here

class Solution {
    
    
public:
    int search(vector<int>& nums, int target) {
    
    
        //二分查找
        int begin = 0;
        int end = nums.size() -1;
        while(end>= begin){
    
    
            int mid = (end + begin) / 2;
            //cout <<"mid="<< mid;
            //找到目标元素则返回
            if(nums[mid] == target) return mid;
            else if(nums[mid] < target){
    
    
                //目标比后半段的最后一个元素都要大,且当前在后半段,则转为查找前半段
                if(target > nums[nums.size() -1] && nums[mid] <= nums[nums.size() -1]){
    
    
                    end = mid - 1;
                    //cout<<"begin = "<< begin<< "end = "<<end<<endl;
                }
                else
                    begin = mid + 1;
                //cout<<" begin = "<< begin<< " end = "<<end<<endl;
            } 
            else {
    
    
                //目标比前半段的第一个元素都要小,且当前在前半段,则转为查找后半段
                if(target < nums[0] && nums[mid] >= nums[0]){
    
    
                    begin = mid + 1;
                    end = nums.size()-1;
                }
                else
                    end = mid - 1;
                //cout<<" begin = "<< begin<< " end = "<<end<<endl;
            }

        }return -1;
    }
};

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44343355/article/details/132892733