代码随想录算法训练营Day45 | 70. 爬楼梯(进阶),322. 零钱兑换,279.完全平方数 | Day 16 复习

70. 爬楼梯(进阶)

文章链接  |  题目链接

C++解法

class Solution {
public:
    int climbStairs(int n) {
        vector<int> dp(n + 1, 0);
        dp[0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 2; j++) {
                if (i - j >= 0) dp[i] += dp[i - j];
            }
        }
        return dp[n];
    }
};

Python解法

class Solution:
    def climbStairs(self, n: int) -> int:
        dp = [0]*(n + 1)
        dp[0] = 1
        m = 2
        for j in range(n + 1):
            for step in range(1, m + 1):
                if j >= step:
                    dp[j] += dp[j - step]
        return dp[n]

322. 零钱兑换

文章链接  |  题目链接  |  视频链接

C++解法

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> dp(amount+1, INT_MAX);
        dp[0] = 0;
        for (int i = 0; i < coins.size(); i++){
            for (int j = coins[i]; j <= amount; j++){
                if (dp[j - coins[i]] != INT_MAX){
                    dp[j] = min(dp[j], dp[j-coins[i]]+1);
                }
            }
        }
        if (dp[amount] == INT_MAX) return -1;
        return dp[amount];
    }
};

Python解法

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        dp = [float('inf')] * (amount+1)
        dp[0] = 0
        for i in range(len(coins)):
            for j in range(coins[i], amount+1):
                if dp[j-coins[i]] != float('inf'):
                    dp[j] = min(dp[j], dp[j - coins[i]]+1)
        if dp[amount] == float('inf'):
            return -1
        else:
            return dp[amount]

279.完全平方数

文章链接  |  题目链接  |  视频链接

C++解法

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp (n+1, INT_MAX);
        dp[0] = 0;
        for (int i = 1; i*i <= n; i++){
            for (int j = i*i; j <= n; j++){
                dp[j] = min(dp[j-i*i]+1, dp[j]);
            }
        }
        return dp[n];
    }
};

Python解法

class Solution:
    def numSquares(self, n: int) -> int:
        dp = [float('inf')] * (n+1)
        dp[0] = 0
        for i in range(1, int(n**0.5)+1):
            for j in range(i*i, n+1):
                dp[j] = min(dp[j-i*i]+1, dp[j])
        return dp[n]

Day 16 复习

104.二叉树的最大深度

/**
 * 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 getDepth(TreeNode* curr){
        if (curr == NULL){
            return 0;
        }
        int left_depth = getDepth(curr->left);
        int right_depth = getDepth(curr->right);
        return 1 + max(left_depth, right_depth);
    }

    int maxDepth(TreeNode* root) {
        return getDepth(root);
    }
};

559.n叉树的最大深度

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    int getHeight(Node* curr){
        if (curr == NULL) return 0;
        int max_height = 0;
        for (int i = 0; i < curr->children.size(); i++){
            max_height = max(max_height, getHeight(curr->children[i]));
        }
        return max_height + 1;
    }

    int maxDepth(Node* root) {
        return getHeight(root);
    }
};

111.二叉树的最小深度

/**
 * 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 getHeight(TreeNode* curr){
        if (curr == NULL) return 0;
        int left_height = getHeight(curr->left);
        int right_height = getHeight(curr->right);
        if (curr->left == NULL && curr->right){
            return 1 + right_height;
        } else if (curr->right == NULL && curr->left){
            return 1 + left_height;
        } else {
            return 1 + min(left_height, right_height);
        }
    }

    int minDepth(TreeNode* root) {
        return getHeight(root);
    }
};

222.完全二叉树的节点个数

/**
 * 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 countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int left_depth = 1, right_depth = 1;
        while (left){
            left = left->left;
            left_depth++;
        }
        while (right){
            right = right->right;
            right_depth++;
        }
        if (left_depth == right_depth){
            return pow(2, left_depth) - 1; 
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值