【LeetCode】1000 Questions Challenge (220/1000)

1000 question challenge

No nonsense, just start brushing!

Table of contents

1000 question challenge

No nonsense, just start brushing!

Question 1: 119. Yang Hui Triangle II - Leetcode

topic interface

problem solving ideas

code:

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

Second question:

​edit

topic interface

problem solving ideas

code:

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

​Edit Question 3: 168. Excel table column names - Leetcode

topic interface

problem solving ideas

code:

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

Question 4: 171. Excel column number - Leetcode

topic interface

problem solving ideas

code:

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

Question screenshot:

Write at the end:


Question 1: 119. Yang Hui Triangle II - Leetcode

topic interface

class Solution {
public:
    vector<int> getRow(int rowIndex) {
​
    }
};

problem solving ideas

My thoughts on this question are:

  1. laws can be deduced mathematically

  2. can be done with recursion

  3. Violently construct the Yang Hui triangle, and then return to the line required by the title

Here I will talk about the idea of ​​recursion: (If you can think of recursion during the interview, then of course use recursion)

We continue to add 1 to the array through the i subscript,

The subscript j is i - 1, which is equal to the sum of your own position plus the previous position,

We recurse from the back to the front, and the result of the recursion will not be affected by changing the value of the array.

code:

class Solution {
public:
    vector<int> getRow(int k) {
        vector<int> ret(k + 1);
        for(int i = 0; i <= k; ++i) { //不断往数组里面添加1
            ret[i] = 1;
            for(int j = i - 1; j >= 1; --j) { //j下标位置加上前一个位置的和
                ret[j] += ret[j - 1];
            }
        }
        return ret;
    }
};

I use brute force.

code show as below:

code:

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        int r = rowIndex + 1;
        vector<vector<int>> vv(r, vector<int>(r, 0)); //构造出足够的空间
        for(int i = 0; i < r; i++) vv[i][0] = 1; //给第一排填上1
        for(int i = 1; i < r; i++) { //构造杨辉三角
            for(int j = 1; j < r; j++) {
                vv[i][j] = vv[i - 1][j] + vv[i - 1][j - 1];
            }
        }
        vector<int> res; //把题目要求的那行输出
        for(int i = 0; i < r; i++) res.push_back(vv[rowIndex][i]);
        return res;
    }
};

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

Second question:

topic interface

class Solution {
public:
    int maxProfit(vector<int>& prices) {
​
    }
};

problem solving ideas

The solution to this problem:

  1. monotonic stack

  2. Dynamic programming can be used

  3. I am using mock

That’s right, I saw a big guy’s idea when I was doing Jianzhi Offer before,

Traverse the array, if today's stock price is greater than yesterday's stock price, record their difference (maintain the maximum value of this difference)

Then let yesterday's stock price cover today's stock price, which can ensure that if tomorrow's stock price rises, we can buy at the lowest price.

Finally, return to our maximum price difference.

code:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size(), m = 0;
        for(int i = 1; i < n; i++) {
            if(prices[i - 1] < prices[i]) { //如果今天的股价大于昨天的股价
                m = max(m, prices[i] - prices[i - 1]); //维护这个差价的最大值
                prices[i] = prices[i - 1]; //让昨天的股价覆盖今天的股价
            }
        }
        return m;
    }
};

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

 Question 3: 168. Excel column names - Leetcode

topic interface

class Solution {
public:
    string convertToTitle(int columnNumber) {
​
    }
};

problem solving ideas

The idea of ​​solving this problem is very important.

We can transform this idea into:

Convert decimal to 26,

In this way, we only need to pay attention, each time before %26, we need to shift the number to the left by 1 as a whole.

code:

class Solution {
public:
    string convertToTitle(int columnNumber) {
        string res;
        while(columnNumber) {
            columnNumber--; //向左偏移
            res += (columnNumber % 26 + 'A'); //进制转换
            columnNumber /= 26; 
        }
        reverse(res.begin(), res.end()); //因为我们是从低位尾插进res的,所以最后需要反转一下
        return res;
    }
};

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

Question 4: 171. Excel column serial number - Leetcode

topic interface

class Solution {
public:
    int titleToNumber(string columnTitle) {
        
    }
};

problem solving ideas

The idea of ​​this question is similar to a question,

This question is 26 base to 10 base.

Here is the code:

code:

class Solution {
public:
    int titleToNumber(string columnTitle) {
        int sum = 0;
        for(int i = 0; i < columnTitle.size(); i++) {
            sum = sum * 26 + (columnTitle[i] - 'A' + 1); 
        }
        return sum;
    }
};

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

Question screenshot:

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 errors 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/130264762