【LeetCode】HOT 100(2)

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: 5. Longest Palindromic Substring - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

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

Topic: 11. The container that holds the most water - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

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

Write at the end:


Title: 5. Longest Palindromic Substring - Leetcode

The interface of the topic:

class Solution {
public:
    string longestPalindrome(string s) {

    }
};

Problem-solving ideas:

I see that many solutions to this question use dynamic programming.

I am not good at dynamic programming, so I used the idea of ​​central diffusion to do this question.

So I will talk about the idea of ​​central diffusion:

Iterating over the array directly,

Take each subscript when traversing as the center and spread to both sides,

Calculate the size of this palindrome string, if the current palindrome string is the longest,

Just update his: start boundary start, end boundary end, length max_len.

Finally, cut out the longest palindrome string with substr and return it.

code show as below:

code:

class Solution {
public:
    string longestPalindrome(string s) {
        int len = s.size();
        if(len <= 1) return s; //特殊情况直接返回
        int start = 0;
        int end = 0;
        int max_len = 0;
        for(int i = 0; i < len; i++) { //遍历字符串
            int len1 = exp_center(s, i, i); //aba情况
            int len2 = exp_center(s, i, i + 1); //abba情况
            max_len = max(max(len1, len2), max_len); //更新最大值
            if(max_len > end - start + 1) { //如果这次是最大值,更新边界
                start = i - ((max_len - 1) / 2); //考虑abba情况的下标,所以要-1
                end = i + (max_len / 2); 
            }
        }
        return s.substr(start, max_len); 
    }
private: //计算当前回文串的大小
    int exp_center(const string& s, int l, int r) {
        while(l >= 0 && r < s.size() && s[l] == s[r]) {
            l--;
            r++;
        }
        return r - l - 1;
    }
};

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

Topic: 11. The container that holds the most water - Leetcode

The interface of the topic:

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

    }
};

Problem-solving ideas:

The idea of ​​solving this question is very ingenious, and I couldn't figure it out at first.

Specifically it looks like this:

We solve it by double pointer,

One pointer on the far left and one pointer on the far right:

Then, comparing the size of the values ​​pointed to by the two pointers,

Update the pointer on the small side:

 Then update the largest area each time,

In simple terms, this principle is:

Every time the pointer range is updated, the length of the water container will definitely become smaller. If you want to hold more water, find the maximum value.

Only by updating the shorter line and continuing to find a higher line can it be possible to increase the water capacity.

The code logic is relatively simple, so I won't add comments.

 code show as below:

code:

class Solution {
public:
    int maxArea(vector<int>& height) {
        int left = 0, right = height.size() - 1;
        int max_val = 0;
        while(left != right) {
            max_val = max(max_val, min(height[left], height[right]) * (right - left));
            if(height[left] < height[right]) {
                left++;
            }
            else right--;
        }
        return max_val;
    }
};

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