【LeetCode】HOT 100(19)

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: 739. Daily Temperature - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 647. Palindromic Substrings - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 739. Daily Temperature - Leetcode

The interface of the topic:

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        
    }
};

Problem-solving ideas:

After taking a look at this question,

Go ahead and do it violently, then,

just timed out:

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        vector<int> v;
        for(int i = 0; i < temperatures.size(); i++) {
            int cnt = 0;
            for(int j = i + 1; j <= temperatures.size(); j++) {
                if(j == temperatures.size()) {
                    v.push_back(0);
                    break;
                }
                cnt++;
                if(temperatures[j] > temperatures[i]) {
                    v.push_back(cnt);
                    break;
                }
            }
        }
        return v;
    }
};

Then it times out:

 In fact, this question needs to be done with a monotonic stack.

(I saw in the problem solution later that the optimized violence seems to be able to pass)

The specific ideas are as follows:

Initialize each element of the returned array to 0

Traverse the array, then push it into the stack, and keep the monotonic stack in a decreasing state,

If a value larger than the top of the stack needs to be pushed into the stack, subtract the subscripts to get the number of days between intervals.

Then let the value at the top of the stack be popped out of the stack.

code show as below:

code:

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        vector<int> ans(temperatures.size(), 0); //返回数组元素初始化成0
        stack<int> st;
        for(int i = 0; i < temperatures.size(); i++) {
            while(!st.empty() && temperatures[i] > temperatures[st.top()]) {
                int top = st.top(); //记录栈顶值下标
                st.pop(); //栈顶出栈
                ans[top] = i - top; //下标相减得距离
            }
            st.push(i); //遍历入栈
        }
        return ans;
    }
};

It's over! ! ! !

Title: 647. Palindromic Substrings - Leetcode

The interface of the topic:

class Solution {
public:
    int countSubstrings(string s) {

    }
};

Problem-solving ideas:

This question is violent at first glance,

The little prince of violence asks to fight,

In the end, you can really use violence,

Take a look at the solution, using the central diffusion,

With all due respect, isn't that violence,

This question must be able to use dynamic programming, but I will not use it,

Let's be violent.

code show as below:

code:

class Solution {
public:
    int countSubstrings(string s) {
        int cnt = 0;
        for(int i = 0; i < s.size(); i++) {
            string tmp;
            for(int j = i; j < s.size(); j++) {
                tmp += s[j];
                if(isHui(tmp)) cnt++;
            }
        }
        return cnt;
    }
private:
    bool isHui(const string& s) {
        int left = 0, right = s.size() - 1;
        while(left <= right) {
            if(s[left] != s[right]) return false;
            left++;
            right--;
        }
        return true;
    }
}; 

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/131283680