【LeetCode】HOT 100(7)

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:

Title: 42. Catching Rainwater - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

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

Title: 46. Full Arrangement - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

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

Write at the end:


Title: 42. Catching Rainwater - Leetcode

The interface of the topic:

class Solution {
public:
    int trap(vector<int>& height) {

    }  
};

Problem-solving ideas:

 This question is also a question of dynamic programming, but it can also be done without dynamic programming.

Here I use a monotonic stack:

Traverse the array, if you encounter a small value, push it into the stack, if you encounter a value larger than the top of the stack, start to calculate the rainwater area,

Keep the valid data in the stack in a monotonically decreasing state,

The core of this idea is to find the taller pillar on the right and start to settle the rainwater.

Then use this pillar as the new left pillar, and continue to the right to find a relatively taller pillar.

Enter the code below:

code:

class Solution {
public:
    int trap(vector<int>& height) {
        int ans = 0;
        stack<int> st;
        for(int i = 0; i < height.size(); i++) { //遍历
            while(!st.empty() && height[st.top()] < height[i]) { //遇到较大的值
                int top = st.top();
                st.pop();
                if(st.empty()) break; //如果是栈内只有一个值的情况,就跳过(没法接雨水)
                int l = st.top();
                int r = i;
                int h = min(height[r], height[l]) - height[top]; //矮柱子的高度 - 中间的高度 
                ans += (r - l - 1) * h;
            }
            st.push(i);
        }
        return ans;
    }  
};

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

Title: 46. Full Arrangement - Leetcode

The interface of the topic:

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {

    }
};

Problem-solving ideas:

This question is actually a classic search question.

Or an introductory topic for search,

Learning search must brush the question type, full arrangement,

I also wrote blogs related to depth-first search when I was writing questions in the Blue Bridge Cup.

If you are interested, you can take a look.

Here I will not talk nonsense, directly on the code:

code:

class Solution {
public:
    vector<int> v;
    vector<vector<int>> vv;
    vector<vector<int>> permute(vector<int>& nums) {
        dfs(nums);
        return vv;
    }
private:
    void dfs(vector<int>& nums) {
        if(v.size() == nums.size()) {
            vv.push_back(v);
            return;
        }
        for(int i = 0; i < nums.size(); i++) {
            if(nums[i] != 99) { //这边是巧妙的记录已经使用过的值,因为排列中只允许一个数出现一次
                int used = nums[i];

                v.push_back(nums[i]);
                nums[i] = 99;
                dfs(nums);
                nums[i] = used;
                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/130944796