[Preferred Algorithm Question Practice] day7


1. 35. Search for insertion position

1. Introduction to the topic

35. Search for insertion position
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

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int searchInsert(vector<int>& nums, int target) {
    
    
        int left = 0, right = nums.size() - 1;
        while(left <= right)
        {
    
    
            int mid = left + (right - left) / 2;
            if(nums[mid] < target)
            {
    
    
                left = mid + 1;
            }
            else if(nums[mid] > target)
            {
    
    
                right = mid - 1;
            }
            else
            {
    
    
                return mid;
            }
        }
        return left;
    }
};

4. Running results

Insert image description here

2. 69. The square root of x

1. Introduction to the topic

69. Square root of x
Given a non-negative integer x, calculate and return the arithmetic square root of x.
Since the return type is an integer, only the integer part of the result is retained, and the decimal part will be rounded off.
Note: Any built-in exponential functions and operators are not allowed, such as pow(x, 0.5) or x ** 0.5.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int mySqrt(int x) {
    
    
        if(x < 1) return 0;
        int left = 1, right = x;
        while(left < right)
        {
    
    
            long long mid = left + (right - left + 1) / 2;
            if(mid * mid <= x)
            {
    
    
                left = mid;
            }
            else
            {
    
    
                right = mid - 1;
            }
        }
        return left;
    }
};

4. Running results

Insert image description here

3. 852. Peak index of mountain array

1. Introduction to the topic

852. Peak index of mountain array
The array arr that meets the following properties is called a mountain array:
arr.length >= 3
exists i (0 < i < arr.length - 1) such that:
arr[0] < arr[1] < … arr[i-1] < arr[i]
arr[i] > arr[i+1] > … > arr[arr.length - 1]
gives you a mountain array arr composed of integers, and returns arr[0] < arr[1] < … arr[i - 1] < arr[i] > arr[i + 1] > … > subscript i of arr[arr.length - 1].
You must design and implement a solution with time complexity O(log(n)).
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int peakIndexInMountainArray(vector<int>& arr) {
    
    
        int left = 1, right = arr.size() - 2;
        while(left < right)
        {
    
    
            int mid = left + (right - left + 1) / 2;
            if(arr[mid] > arr[mid - 1])//如果mid所处位置是上升的,则我们要在mid + 1~right区间寻找
            {
    
    
                left = mid;
            }
            else//如果mid所处位置是下降的,则我们要在left~mid-1区间寻找
            {
    
    
                right = mid - 1;
            }
        }
        return left;
    }
};

4. Running results

Insert image description here


Summarize

Today is the 7th day of algorithm practice.
As long as you work hard and grind the iron pestle into a needle , keep working hard!
Source of the question: LeetCode, the copyright belongs to LeetCode.
If this article has inspired you, I hope you can support the author more, thank you all!

Guess you like

Origin blog.csdn.net/xjjxjy_2021/article/details/131745729