Leetcode brushes the daily problem and the next problem of the daily problem notes 24/30

Writing in the front

This is the 24th day of my participation in Gwen Challenge

About to graduate, only to find himself in the interview algorithm hanging hammer. There is no way to zero based identity and classmates to join the force buckle brush problem army. My classmates are very good, they are usually just modest, verbally said that they will not, and I really will not… Nuggets encourage new people to blog every day, I also join in a lively, record every day brush the first two questions, these two questions I do. I plan to brush five questions every day. For the other questions, I can only memorize the routine by force and will not post them in my blog.

I am really just a vegetable chicken, what do not want to solve the problem from my reference, coding habits also need to improve, if you want to find brush problem master ask questions, I think it is better to go to the palace water sanye brush problem diary this big guy. I try not to see the solution of the problem before I make it, so as not to collide with the content of the big guy.

In addition, I also hope to have the leisure of the big guy to provide some higher clear problem-solving ideas to me, welcome to discuss ha!

Good nonsense not much to say the first two questions of the twenty-fourth day!

2021.6.24 One question of the day

149. Maximum number of points on a line

I’m not going to hash this one out. Okay, it’s better than enumerating violence, but it’s not that good. The hash table stores the slope, there is a pit, the slope of the line parallel to the coordinate axis is 0 or not there, there is also the eight immortal crossing the sea.


class Solution {
public:
    int maxPoints(vector<vector<int>>& points) {
        int len = points.size(a);if (len < 3) {
            return len;
        }
        int maxNum = 2;
        for (int i = 0; i < len; i++) {
            unordered_map<double.int> count;
            for (int j = 0; j < len; j++) {
                if(i ! = j) {long long dx = points[i][0] - points[j][0];
                    long long dy = points[i][1] - points[j][1];
                    double gradient = dy * 1.0 / dx;
                    if (count.count(gradient)) {
                        count[gradient] ++;
                    } else {
                        count[gradient] = 2;
                    }
                    maxNum = max(maxNum, count[gradient]); }}}returnmaxNum; }};Copy the code

One of the following questions per day

20. Valid brackets

This one uses a stack, or you can do a window, but there’s a variation on this, which is to use prefixes and alpha, which we talked about earlier. This problem is simple enough, directly stack finished.


class Solution {
public:
    bool isValid(string s) {
        int n = s.size(a);if (n % 2= =1) {
            return false;
        }
        stack<char> stk;
        for(int i = 0; i < n; i++) {
            if (s[i] == '(') {
                stk.push(') ');
            } else if (s[i] == '{') {
                stk.push('} ');
            } else if (s[i] == '[') {
                stk.push('] ');
            } else if(! stk.empty() && s[i] == stk.top()) {
                stk.pop(a); }else {
                return false; }}return stk.empty()?true:false; }};Copy the code

summary

Hash table to reduce complexity is just a way of optimization, the first problem has many optimization methods, knowledge is very broken. The second question examines the concept of stack.

Refer to the link

There is no