【LeetCode】HOT 100(6)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Topic: 34. Find the first and last position of an element in a sorted array - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over, it's over! ! ! !

Title: 39. Combination Sum - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over, it's over! ! ! !

Write at the end:


Topic: 34. Find the first and last position of an element in a sorted array - Leetcode

The interface of the topic:

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {

    }
};

Problem-solving ideas:

This question takes a look at the question, and then sees the time complexity requirements,

You will know right away that violence cannot pass, you must have two points,

The general idea is to use binary search to find the left and right boundaries.

The specific ideas are as follows:

Find the position of the target required by the question through binary search (nums[mid] == target)

Then there are two cases,

If it is looking for the left boundary, it will always update the right boundary close to the past

If you are looking for the right boundary, keep updating the left boundary to get past

code show as below:

code:

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        return {search_range(nums, target, "left"), search_range(nums, target, "right")};
    }
private:
    int search_range(const vector<int>& nums, int target, const string& side) {
        int left = 0, right = nums.size() - 1;
        int res = -1;
        while(left <= right) {
            int mid = (left + right) >> 1;
            if(nums[mid] < target) { //经典二分
                left = mid + 1;
            }
            else if(nums[mid] > target) {
                right = mid - 1;
            }
            else { //nums[mid] == target
                res = mid;
                if(side == "left") { //更新右边界靠过去
                    right = mid - 1;
                }
                if(side == "right") { //更新左边界靠过去
                    left = mid + 1;
                }
            }
        }
        return res;
    }
};

It's over, it's over! ! ! !

Title: 39. Combination Sum - Leetcode

The interface of the topic:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {

    }
};

Problem-solving ideas:

He said that the number of combinations for this question does not exceed 150, and he immediately thought that this question cannot be won by violence.

There seems to be some problems with direct violence. The same number can be selected multiple times, which is not easy to achieve with loops.

At this time, I thought of searching. It is not a big problem for dfs to win this question.

code show as below:

code:

class Solution {
public:
    vector<int> v; //每个合格的数组(sum == target)
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> res; //存返回的数组
        dfs(candidates, res, target, 0, 0); //搜索
        return res;
    }
private:
    void dfs(const vector<int>& candidates, vector<vector<int>>& res,
            int target, int sum, int begin) {
        if(sum == target) { //符合条件
            res.push_back(v);
            return;
        }
        if(sum > target) return; //剪枝
        for(int i = begin; i < candidates.size(); i++) { //搜索
            v.push_back(candidates[i]);
            dfs(candidates, res, target, sum + candidates[i], i);
            v.pop_back(); //回溯
        }
    }
};

It's over, it's over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/130925941