483-82(23、239、450、113)

23. 合并K个升序链表

在这里插入图片描述

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        if (lists.size() == 0)   return nullptr;

        ListNode* dummy_node = new ListNode(-1);
        ListNode* cur = dummy_node;

        auto cmp = [](ListNode* a, ListNode* b)->bool {return a->val > b->val; };
        priority_queue<ListNode*, vector<ListNode*>, function<bool(ListNode* a, ListNode* b)>> q(cmp);

        for (ListNode* list : lists)
        {
            if (list == nullptr) continue;
            q.push(list);
        }

        while (!q.empty())
        {
            ListNode* temp = q.top();
            q.pop();
            cur->next = temp;
            cur = cur->next;
            if (temp->next != nullptr)
            {
                q.push(temp->next);
            }
        }
        return dummy_node->next;
    }
};

在这里插入图片描述

239. 滑动窗口最大值

在这里插入图片描述

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();

        priority_queue<pair<int, int>> q;

        for (int i = 0; i < k; i++)
        {
            q.push({ nums[i], i });
        }

        vector<int> ans;
        ans.push_back(q.top().first);

        for (int i = k; i < n; i++)
        {
            q.push({ nums[i], i });

            while (q.top().second <= i - k)
            {
                q.pop();
            }
            ans.push_back(q.top().first);
        }
        return ans;
    }
};

450. 删除二叉搜索树中的节点

在这里插入图片描述

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == nullptr) return root;

        if (root->val == key)
        {
            if (root->left == nullptr && root->right == nullptr)
            {
                delete root;
                return nullptr;
            }
            else if (root->left == nullptr)
            {
                TreeNode* right_node = root->right;
                delete root;
                return right_node;
            }
            else if (root->right == nullptr)
            {
                TreeNode* left_node = root->left;
                delete root;
                return left_node;
            }
            else
            {
                TreeNode* cur = root->right;
                while (cur->left != nullptr)
                {
                    cur = cur->left;
                }
                cur->left = root->left;
                TreeNode* temp = root->right;
                delete root;
                return temp;
            }
        }
        if (root->val > key) root->left = deleteNode(root->left, key);
        if (root->val < key) root->right = deleteNode(root->right, key);
        return root;
    }
};

113. 路径总和 II

在这里插入图片描述

class Solution {
private:
    vector<vector<int>> res;
    vector<int> path;
public:
    void traversal(TreeNode* cur, int count)
    {
        if (cur->left == nullptr && cur->right == nullptr && count == 0)
        {
            res.push_back(path);
            return;
        }

        if (cur->left)
        {
            path.push_back(cur->left->val);
            count -= cur->left->val;
            traversal(cur->left, count);
            count += cur->left->val;
            path.pop_back();
        }
        
        if (cur->right)
        {
            path.push_back(cur->right->val);
            count -= cur->right->val;
            traversal(cur->right, count);
            count += cur->right->val;
            path.pop_back();
        }
        return;
    }

public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        res.clear();
        path.clear();

        if (root == nullptr) return res;

        path.push_back(root->val);

        traversal(root, targetSum - root->val);
        return res;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufeng2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值