【LeetCode】HOT 100(24)

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: 437. Path Sum III - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 416. Splitting Equal Sum Subsets - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 437. Path Sum III - 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 pathSum(TreeNode* root, int targetSum) {

    }
};

Problem-solving ideas:

For this question, I just use depth-first search directly.

Then find the path sum through the rootSum function,

Find the path sum of different root nodes by repeatedly calling pathSum itself,

The core is to complete path summation by reducing targetSum each time rootSum is called.

code show as below:

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:
    long pathSum(TreeNode* root, long targetSum) {
        if(root == nullptr) return 0;
        long ret = rootSum(root, targetSum);
        ret += pathSum(root->left, targetSum);
        ret += pathSum(root->right, targetSum);
        return ret;
    }
    
private:
    long rootSum(TreeNode* root, long targetSum) {
        if(root == nullptr) return 0;
        long ret = 0;
        if(root->val == targetSum) ret++;
        ret += rootSum(root->left, targetSum - root->val);
        ret += rootSum(root->right, targetSum - root->val);
        return ret;
    }
};

It's over! ! ! !

Title: 416. Splitting Equal Sum Subsets - Leetcode

The interface of the topic:

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

    }
};

Problem-solving ideas:

The orthodox solution to this problem should be dynamic programming,

But I don't know dynamic programming, and then I saw an alternative solution in the discussion area,

The specific idea is this:

Because it is divided into two subsets, and they are all integers, so their sum is actually half of the sum.

Compare all the additions with half of the sum, and then you can see whether it can be divided into two subsets,

code show as below:

code:

class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for(auto e : nums) sum += e;
        if(sum % 2 != 0) return false;
        int target = sum / 2;
        set<int> st;
        for(int i = 0; i < nums.size(); i++) {
            set<int> snum;
            if(nums[i] == target) return true;
            if(nums[i] < target) snum.insert(nums[i]);
            for(auto num : st) {
                int tarSum = nums[i] + num;
                if(tarSum == target) return true;
                if(tarSum < target) snum.insert(tarSum);
            }
            for(auto k : snum) st.insert(k);
        }
        return false;
    }
};

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