Leetcode刷题笔记--Hot91--100

目录

1--汉明距离(461)

2--目标和(494)

3--把二叉搜索树转换为累加树(538)

4--二叉树的直径(543)

5--和为L的子数组(560)

6--最短无序连续子数组(581)

7--合并二叉树(617)

8--任务调度器(621)

9--回文子串(647)

10--每日温度(739)


1--汉明距离(461)

Leetcode刷题笔记--Hot91--100_第1张图片

主要思路:

      按位异或,统计1的个数;  

#include 
#include 

class Solution {
public:
    int hammingDistance(int x, int y) {
        int z = x ^ y; // 按位异或
        int res = 0;
        while(z){
            if((z % 2) == 1) res++;
            z = z >> 1; // 右移
        }
        return res;
    }
};

int main(int argc, char* argv[]){
    // x = 1, y = 4
    int x = 1, y = 4;
    Solution S1;
    int res = S1.hammingDistance(x, y);
    std::cout << res << std::endl;
    return 0;
}

2--目标和(494)

Leetcode刷题笔记--Hot91--100_第2张图片

主要思路:
        转化为 0-1 背包问题,一部分数值连同 target 转化为背包容量,剩余一部分数值转化为物品,求解恰好装满背包容量的方法数;

        dp[j] 表示背包容量为 j 时,装满背包的方法数;

        状态转移方程:dp[j] += dp[j - nums[i]],其实质是:当背包已经装了nums[i]时,剩余容量为 j - nums[i],此时装满剩余容量的方法数为 dp[j - nums[i]],遍历不同的 nums[i] 将方法数相加即可;

        是有点难理解。。。

#include 
#include 
 
class Solution {
public:
    int findTargetSumWays(std::vector& nums, int target) {
        int sum = 0;
        for(int num : nums) sum += num;
        if(sum < std::abs(target)) return 0; // 数组全部元素相加相减都不能构成target
        if((sum + target) % 2 == 1) return 0; // 不能二等分
 
        int bagsize = (sum + target) / 2;
        std::vector dp(bagsize + 1, 0);
        dp[0] = 1;
        for(int i = 0; i < nums.size(); i++){ // 遍历物品
            for(int j = bagsize; j >= nums[i]; j--){ // 遍历背包容量
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[bagsize];
    }
};
 
int main(int argc, char *argv[]) {
    // nums = [1, 1, 1, 1, 1], target = 3
    std::vector test = {1, 1, 1, 1, 1};
    int target = 3;
    Solution S1;
    int res = S1.findTargetSumWays(test, target);
    std::cout << res << std::endl;
	return 0;
}

3--把二叉搜索树转换为累加树(538)

Leetcode刷题笔记--Hot91--100_第3张图片

主要思路:

        二叉树递归,本题的遍历顺序应该为 右→根→左;逐层向下传递数据,同时回溯逐层向上返回数据;

#include 
#include 
#include 

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:
    TreeNode* convertBST(TreeNode* root){
        dfs(root, 0);
        return root;
    }
    int dfs(TreeNode* root, int sum){
        if(root == nullptr) return sum;
        // 右
        int right = dfs(root->right, sum);
        // 根
        root->val = root->val + right;
        // 左
        int left = dfs(root->left, root->val);
        return left;
    }
};

int main(int argc, char* argv[]){
    // [4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8]
    TreeNode* Node1 = new TreeNode(4);
    TreeNode* Node2 = new TreeNode(1);
    TreeNode* Node3 = new TreeNode(6);
    TreeNode* Node4 = new TreeNode(0);
    TreeNode* Node5 = new TreeNode(2);
    TreeNode* Node6 = new TreeNode(5);
    TreeNode* Node7 = new TreeNode(7);
    TreeNode* Node8 = new TreeNode(3);
    TreeNode* Node9 = new TreeNode(8);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5; 
    Node3->left = Node6;
    Node3->right = Node7;
    Node5->right = Node8;
    Node7->right = Node9;

    Solution S1;
    TreeNode* res = S1.convertBST(Node1);
    // 层次遍历打印
    std::queue q;
    q.push(res);
    while(!q.empty()){
        TreeNode* top = q.front();
        q.pop();
        if(top != nullptr){
            std::cout << top->val << " ";
        }
        else{
            std::cout << "null" << " ";
        }
        if(top != nullptr){
            q.push(top->left);
            q.push(top->right);
        }
    }
    return 0;
}

4--二叉树的直径(543)

Leetcode刷题笔记--Hot91--100_第4张图片

主要思路:

        递归二叉树,对于每一个节点,递归求解左子树的结点数,右子树的结点数,向上返回最大的节点树;

        二叉树的直径等于某一个节点左右子树最大路径的总结点数,例如上图中[4, 2, 1, 3]可以理解为以 1 为根节点,其左子树路径的最大结点数为[4, 2],右子树路径的最大结点数为[3];

#include 
#include 

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 diameterOfBinaryTree(TreeNode* root) {
        int res = 1; // 路径经过的最大结点数
        dfs(root, res);
        return res - 1; // res - 1 即为直径
    }

    int dfs(TreeNode* root, int &res){
        if(root == nullptr) return 0;
        int left = dfs(root->left, res);
        int right = dfs(root->right, res);
        res = std::max(res, left + right + 1);
        return std::max(left, right) + 1;
    }
};

int main(int argc, char* argv[]){
    // root = [1, 2, 3, 4, 5]
    TreeNode* Node1 = new TreeNode(1);
    TreeNode* Node2 = new TreeNode(2);
    TreeNode* Node3 = new TreeNode(3);
    TreeNode* Node4 = new TreeNode(4);
    TreeNode* Node5 = new TreeNode(5);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;

    Solution S1;
    int res = S1.diameterOfBinaryTree(Node1);
    std::cout << res << std::endl;
    return 0;
}

5--和为L的子数组(560)

Leetcode刷题笔记--Hot91--100_第5张图片

主要思路:

        Leetcode刷题笔记--Hot91--100_第6张图片

#include 
#include 
#include 

class Solution {
public:
    int subarraySum(std::vector& nums, int k){
        std::unordered_map hash_map;
        hash_map[0] = 1;
        int pre = 0; // 前缀和
        int res = 0; // 返回的数目
        for(auto num : nums){
            pre += num; // 截止到当前节点的累加和
            if(hash_map.find(pre - k) != hash_map.end()){ // 存在hash_map[pre - k]的节点j,即从节点j开始到当前节点的累加和为k,匹配
                res += hash_map[pre - k];
            }
            hash_map[pre]++; // 累加和为pre的节点数+1
        }
        return res;
    }
};

int main(int argc, char* argv[]){
    // nums = [1, 1, 1], k = 2
    std::vector test = {1, 1, 1};
    int k = 2;
    Solution S1;
    int res = S1.subarraySum(test, k);
    std::cout << res << std::endl;
    return 0;
}

6--最短无序连续子数组(581)

Leetcode刷题笔记--Hot91--100_第7张图片

主要思路:

        

7--合并二叉树(617)

Leetcode刷题笔记--Hot91--100_第8张图片

主要思路:

        经典二叉树递归;

#include 
#include 
#include 
#include 

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:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(root1 == nullptr) return root2;
        if(root2 == nullptr) return root1;
        TreeNode* root = new TreeNode(root1->val + root2->val);
        root->left = mergeTrees(root1->left, root2->left);
        root->right = mergeTrees(root1->right, root2->right);
        return root;
    }
};

int main(int argc, char* argv[]){
    // root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(3);
    TreeNode *Node3 = new TreeNode(2);
    TreeNode *Node4 = new TreeNode(5);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;

    TreeNode *Node5 = new TreeNode(2);
    TreeNode *Node6 = new TreeNode(1);
    TreeNode *Node7 = new TreeNode(3);
    TreeNode *Node8 = new TreeNode(4);
    TreeNode *Node9 = new TreeNode(7);

    Node5->left = Node6;
    Node5->right = Node7;
    Node6->right= Node8;
    Node7->right = Node9;

    Solution S1;
    TreeNode *res = S1.mergeTrees(Node1, Node5);
    std::queue q;
    q.push(res);
    while(!q.empty()){
        TreeNode* cur = q.front();
        q.pop();
        if(cur != nullptr){
            std::cout << cur->val << " ";
            q.push(cur->left);
            q.push(cur->right);
        }
        else{
            std::cout << "null" << " ";
        }
    } 
    return 0;
}

8--任务调度器(621)

Leetcode刷题笔记--Hot91--100_第9张图片

主要思路:

        

9--回文子串(647)

Leetcode刷题笔记--Hot91--100_第10张图片

主要思路:

        

10--每日温度(739)

Leetcode刷题笔记--Hot91--100_第11张图片

主要思路:

        

你可能感兴趣的:(leetcode)