【LeetCode】HOT 100(21)

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: 560. Subarrays whose sum is K - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 543. Diameter of Binary Tree - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 560. Subarrays whose sum is K - Leetcode

The interface of the topic:

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {

    }
};

Problem-solving ideas:

When encountering such a problem,

Regardless of the three seven twenty-one, immediately try violently to see if you can finish the sample:

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int cnt = 0;
        for(int i = 0; i < nums.size(); i++) {
            int sum = 0;
            for(int j = i; j < nums.size(); j++) {
                sum += nums[j];
                if(sum == k) cnt++;
            }
        }
        return cnt;
    }
};

Ok fails with:

 Then there is no way to solve it with violence.

Honestly think about how to optimize this algorithm,

This question can be done with prefix and + hash,

The specific ideas are as follows:

Wait, the official solution has a detailed video explanation,

The narrator is a young lady with a very pleasant voice. I strongly recommend you to listen to it yourself.

Then I won’t explain the idea here, portal: 560. The subarray whose sum is K - Leetcode

code show as below:

code:

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        mp[0] = 1;
        int cnt = 0, preSum = 0;
        for(const auto& e : nums) {
            preSum += e;
            if(mp.find(preSum - k) != mp.end()) {
                cnt += mp[preSum - k];
            }
            mp[preSum]++;
        }
        return cnt;
    }
};

It's over! ! ! !

Title: 543. Diameter of Binary Tree - Leetcode

The interface of the topic:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans = 0;
    int diameterOfBinaryTree(TreeNode* root) {

    }
};

Problem-solving ideas:

A long-lost simple question,

The idea of ​​this question is not difficult. According to the meaning of the question,

Find the longest diameter, it is not difficult to think of,

Finding the longest diameter of a tree is actually finding the diameter of the tree,

The maximum depth of the left subtree of each subtree + the maximum depth of the right subtree (of course you can also say the maximum height)

Then the maximum value among them is actually the maximum diameter of the tree,

Look at the code:

code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans = 0;
    int diameterOfBinaryTree(TreeNode* root) {
        depth(root);
        return ans;
    }
private:
    int depth(TreeNode* root) {
        if(root == nullptr) return 0;
        int left = depth(root->left);  //左子树的最大深度
        int right = depth(root->right);//右子树的最大深度
        ans = max(ans, left + right);  //找出直径最长的子树
        return max(left, right) + 1;   //这个是查找最大深度
    }
};

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