365天挑战LeetCode1000题——Day 085 修剪二叉搜索树 省份数量 连通网络的操作次数 最小面积矩形

669. 修剪二叉搜索树

在这里插入图片描述

代码实现(自解)

/**
 * 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:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if (!root) return nullptr;
        int val = root->val;
        if (val < low) {
            return trimBST(root->right, low, high);
        }
        if (val > high) {
            return trimBST(root->left, low, high);
        }
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);
        return root;
    }
};

547. 省份数量

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    int findCircleNum(vector<vector<int>>& isConnected) {
        set<int> connected;
        int count = 0;
        queue<int> myQueue;
        int n = isConnected.size();
        for (int i = 0; i < n; i++) {
            if (connected.count(i)) continue;
            connected.emplace(i);
            count++;
            myQueue.push(i);
            while (!myQueue.empty()) {
                int tmp = myQueue.front();
                myQueue.pop();
                for (int j = 0; j < n; j++) {
                    if (isConnected[tmp][j] && !connected.count(j)) {
                        myQueue.push(j);
                        connected.emplace(j);
                    }
                }
            }
        }
        return count;
    }
};

1319. 连通网络的操作次数

在这里插入图片描述

代码实现(自解)

class Solution {
private:
    vector<int> parent;
public:
    int myFind(int i) {
        if (parent[i] != i) {
            i = myFind(parent[i]);
        }
        return i;
    }
    void myUnion(int i, int j) {
        parent[myFind(i)] = parent[myFind(j)];
    }

    int makeConnected(int n, vector<vector<int>>& connections) {
        if (n - 1 > connections.size()) return -1;
        parent = vector<int>(n);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
        for (auto arr : connections) {
            myUnion(arr[0], arr[1]);
        }
        int count = -1;
        for (int i = 0; i < n; i++) {
            count += (parent[i] == i);
        }
        return count;
    }
};

939. 最小面积矩形

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    int minAreaRect(vector<vector<int>>& points) {
        int ans = INT_MAX;
        map<int, set<int>> rowMap;
        for (auto& arr : points) {
            rowMap[arr[0]].emplace(arr[1]);
        }
        for (auto &[row, s] : rowMap) {
            if (s.size() == 1) continue;
            for (auto it = s.begin(); it != s.end(); it++) {
                auto it2 = it;
                int col1 = *it;
                for (it2++; it2 != s.end(); it2++) {
                    int col2 = *it2;
                    for (auto &[row1, s1] : rowMap) {
                        if (row1 == row) continue;
                        if (s1.count(col1) && s1.count(col2)) {
                            ans = min(ans, abs((row1 - row) * (col2 - col1)));
                        }
                    }
                }
            }
        }
        return (ans == INT_MAX) ? 0 : ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值