【LeetCode】HOT 100(27)

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: 338. Counting Bits - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 198. Robbery - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

​Edit some words 

Write at the end:


Title: 338. Counting Bits - Leetcode

The interface of the topic:

class Solution {
public:
    vector<int> countBits(int n) {

    }
};

Problem-solving ideas:

This question is not difficult,

He just asks for the number of binary 1s per bit,

and just go back,

Traverse it once, and then design a method to find the number of 1,

code show as below:

code:

class Solution {
public:
    vector<int> countBits(int n) {
        vector<int> ans; 
        for(int i = 0; i <= n; i++) {
            int res = get(i);
            ans.push_back(res);
        }
        return ans;
    }
private:
    int get(int n) {
        int sum = 0;
        while(n) {
            sum += n & 1;
            n >>= 1;
        }
        return sum;
    }
};

It's over! ! ! !

Title: 198. Robbery - Leetcode

The interface of the topic:

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

    }
};

Problem-solving ideas:

Robbery is a very classic dynamic programming introductory topic.

It can very well reflect the idea of ​​dynamic programming,

This is a must-do topic if you want to learn dynamic programming.

The specific ideas are as follows:

The state transition equation is this:

How to decide whether to steal the family,

max (the accumulative amount of the last one stolen, the accumulative amount of the last two + the current amount of this one)

You can find the maximum value of the stolen amount at the current position, or the optimal solution,

code show as below:

code:

class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size() == 0) return 0;
        if(nums.size() == 1) return nums[0];
        vector<int> v(nums.size() + 1);
        v[0] = nums[0];
        v[1] = max(nums[0], nums[1]);
        for(int i = 2; i < nums.size(); i++) {
            v[i] = max(v[i - 1], v[i - 2] + nums[i]);
        }
        return v[nums.size() - 1];
    }
};

It's over! ! ! !

some words 

It's summer vacation, and I have 84 questions in my LeetCode hot100.

The benefits of swiping further down are not so big,

So I plan to start a new topic of brushing questions, and spend this summer vacation to brush up the topics of dynamic programming.

As for the question list, I plan to use the special assault version of Jianzhi Offer.

If there is a chance in the future, I will finish all the hot100 questions.

Then there will be a period later.

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