【每日Leetcode】BFS系列(一)

文摘   2024-07-07 17:55   上海  
【二叉树的层序遍历
/** * 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:    vector<vector<int>> levelOrder(TreeNode* root) {        queue<TreeNode*> q;        q.push(root);        vector<vector<int>> res;        if (!root) return res;        while (!q.empty()) {            int len = q.size();            vector<int> tmp;            for (int i = 0; i < len; ++i) {                TreeNode* front = q.front();                q.pop();                tmp.push_back(front->val);                if (front->left) q.push(front->left);                if (front->right) q.push(front->right);            }            res.push_back(tmp);        }        return res;    }};

【286. 墙与门

你被给定一个 m × n 的二维网格 rooms ,网格中有以下三种可能的初始化值:

-1 表示墙或是障碍物

0 表示一扇门

INF 无限表示一个空的房间。然后,我们用 231 - 1 = 2147483647 代表 INF。你可以认为通往门的距离总是小于 2147483647 的。

你要给每个空房间位上填上该房间到 最近门的距离 ,如果无法到达门,则填 INF 即可。

来源:力扣(LeetCode)

链接:https://leetcode.cn/problems/walls-and-gates/

class Solution {public:    void wallsAndGates(vector<vector<int>>& rooms) {        if (rooms.empty()) return;        vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};        int m = rooms.size();        int n = rooms[0].size();        queue<pair<int, int>> q;        for (int i = 0 ;i < m; ++i) {            for (int j = 0; j < n; ++j) {                if (rooms[i][j] == 0) {                    q.emplace(i, j);                }            }        }        while (!q.empty()) {            int x;            int y;            tie(x, y) = q.front();            q.pop();            for (auto& d: directions) {                int newX = x + d[0];                int newY = y + d[1];                // if (newX < 0 || newY < 0 || newX >= m || newY >= n || rooms[newX][newY] == -1)                if (newX < 0 || newY < 0 || newX >= m || newY >= n || rooms[newX][newY] != INT_MAX)                    continue;                rooms[newX][newY] = rooms[x][y] + 1;                q.emplace(newX, newY);            }        }    }};

【994. 腐烂的橘子

在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:

  • 值 0 代表空单元格;

  • 值 1 代表新鲜橘子;

  • 值 2 代表腐烂的橘子。

每分钟,腐烂的橘子 周围 4 个方向上相邻 的新鲜橘子都会腐烂。

返回 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1 。

来源:力扣(LeetCode)

链接:https://leetcode.cn/problems/rotting-oranges/description/

class Solution {public:    vector<vector<int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};    int orangesRotting(vector<vector<int>>& grid) {        queue<pair<int, int>> q;        int fresh = 0;        int m = grid.size();        int n = grid[0].size();        for (int i = 0; i < m; ++i) {            for (int j = 0; j < n; ++j) {                if (grid[i][j] == 2) {                    q.emplace(i, j);                } else if (grid[i][j] == 1) {                    fresh++;                }            }        }        int res = 0;        // while (!q.empty()) {        while (fresh > 0 && !q.empty()) {            res++;            //队列中现有的所有腐烂橘子都要进行一次感染            int size = q.size();            for (int s = 0; s < size; ++s) {                int x, y;                tie(x, y) = q.front();                q.pop();                for (int i = 0; i < 4; ++i) {                    int newX = x + dir[i][0];                    int newY = y + dir[i][1];                    if (newX >= 0 && newX < m && newY >= 0 && newY < n && grid[newX][newY] == 1) {                        grid[newX][newY] = 2; // 注意!                        fresh--;                        q.emplace(newX, newY);                    }                }            }        }        if (fresh > 0) return -1;        return res;    }};

Arrietty刷题日记
清华计算机系学霸带你刷Leetcode(求职面试/保研考研/转计算机行业...)