二叉树层序遍历
vector<vector<int> > levelOrder(TreeNode* root) {// write code herevector<int> res;vector<vector<int>> result;if (root == nullptr) return result;queue<TreeNode*> que;que.push(root);while (!que.empty()) {int size = que.size();for (int i = 0; i < size; i++) {TreeNode* temp = que.front();que.pop();res.push_back(temp->val);if (temp->left) que.push(temp->left);if (temp->right) que.push(temp->right);}result.push_back(res);res.clear();}return result;}
矩阵最长递增路径
https://www.nowcoder.com/share/jump/9321389651694076681305
BFS 通常是为了找到最短路径,求最长路径最好用DFS!
拓扑排序(增加inDegrees矩阵)+ BFS
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};vector<vector<int>> getInDegrees(vector<vector<int> >& grid) {int m = grid.size();int n = grid[0].size();vector<vector<int>> inDegrees(m, vector<int> (n, 0));for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {for (int k = 0; k < 4; k++) {int newX = i + dir[k][0];int newY = j + dir[k][1];if (newX < 0 || newX >= m || newY < 0 || newY >= n) continue;if (grid[i][j] > grid[newX][newY]) {inDegrees[i][j]++;}}}}return inDegrees;}int solve(vector<vector<int> >& matrix) {// write code herevector<vector<int>> inDegrees = getInDegrees(matrix);int maxLen = 0;queue<pair<int, int>> que;for (int i = 0; i < matrix.size(); i++) {for (int j = 0; j < matrix[0].size(); j++) {if (inDegrees[i][j] == 0) {que.push({i, j});}}}while (!que.empty()) {maxLen++;int size = que.size();for (int i = 0; i < size; i++) {//需要处理每层信息时这样写,类似于二叉树的层序遍历int x = que.front().first;int y = que.front().second;que.pop();for (int k = 0; k < 4; k++) {//遍历方向int newX = x + dir[k][0];int newY = y + dir[k][1];if (newX < 0 || newX >= matrix.size() || newY < 0 ||newY >= matrix[0].size()) continue;if (matrix[x][y] < matrix[newX][newY]) {//保证是递增序列inDegrees[newX][newY]--;//因为已经确保递增了,所以减少newX和newY的一个入度if (inDegrees[newX][newY] == 0) {//当入度全为0,表示条件全满足,所以可以入队que.push({newX, newY});}}}}}return maxLen;}
};
单纯的bfs:
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};int bfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int x,int y) {queue<pair<int, int>> que;que.push({x, y});visited[x][y] = true;int maxLen = 0;while (!que.empty()) {maxLen++;int size = que.size();for (int i = 0; i < size; i++) {//层处理int curX = que.front().first;int curY = que.front().second;que.pop();for (int k = 0; k < 4; k++) {//方向处理int newX = curX + dir[k][0];int newY = curY + dir[k][1];if (newX < 0 || newX >= matrix.size() || newY < 0 ||newY >= matrix[0].size()) continue;if (!visited[newX][newY] && matrix[curX][curY] < matrix[newX][newY]) {que.push({newX, newY});visited[newX][newY] = true;}}}}return maxLen;}int solve(vector<vector<int> >& matrix) {// write code herevector<vector<bool>> visited(matrix.size(), vector<bool>(matrix[0].size(),false));int maxLen = 0;for (int i = 0; i < matrix.size(); i++) {for (int j = 0; j < matrix[0].size(); j++) {maxLen = max(maxLen, bfs(matrix, visited, i, j));}}return maxLen;}};
dfs+记忆化搜索(memo):
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};int dfs(vector<vector<int>>& matrix, vector<vector<int>>& memo, int x, int y) {if (memo[x][y] != -1) return memo[x][y];//递归终止条件int maxLen = 1;for (int i = 0; i < 4; i++) {//遍历方向int newX = x + dir[i][0];int newY = y + dir[i][1];if (newX < 0 || newX >= matrix.size() || newY < 0 || newY >= matrix[0].size() ||matrix[newX][newY] <= matrix[x][y]) continue;//满足条件才递归maxLen = max(maxLen, 1 + dfs(matrix, memo, newX, newY));//表示从(x, y)到(newX, newY)这一步}memo[x][y] = maxLen;return memo[x][y];}int solve(vector<vector<int> >& matrix) {vector<vector<int>> memo(matrix.size(), vector<int>(matrix[0].size(), -1));int maxLen = 0;for (int i = 0; i < matrix.size(); i++) {for (int j = 0; j < matrix[0].size(); j++) {maxLen = max(maxLen, dfs(matrix, memo, i, j));}}return maxLen;}
};
被围绕的区域
https://www.nowcoder.com/share/jump/9321389651694087623428
dfs:
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};void dfs(vector<vector<char>>& matrix, int x,int y) {int m = matrix.size(), n = matrix[0].size();matrix[x][y] = 'E';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 ||matrix[newX][newY] != 'O')continue;//(newX,newY)中超出边界的、不是O的不用管dfs(matrix, newX, newY);}}vector<vector<char> > surroundedArea(vector<vector<char> >& board) {// write code hereint m = board.size(), n = board[0].size();//将连接边界的O全部替换for (int i = 0; i < m; i++) {if (board[i][0] == 'O') dfs(board, i, 0);if (board[i][n - 1] == 'O') dfs(board, i, n - 1);}for (int j = 0; j < n; j++) {if (board[0][j] == 'O') dfs(board, 0, j);if (board[m - 1][j] == 'O') dfs(board, m - 1, j);}//又替换回来for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (board[i][j] == 'E') board[i][j] = 'O';else board[i][j] = 'X';}}return board;}
};
bfs:
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};void bfs(vector<vector<char>>& matrix, int x, int y) {int m = matrix.size(), n = matrix[0].size();queue<pair<int, int>> que;que.push({x, y});while (!que.empty()) {int curX = que.front().first;int curY = que.front().second;que.pop();matrix[curX][curY] = 'E';for (int i = 0; i < 4; i++) {int newX = curX + dir[i][0];int newY = curY + dir[i][1];if (newX < 0 || newX >= m || newY < 0 || newY >= n ||matrix[newX][newY] != 'O')continue;//(newX,newY)中超出边界的、不是O的不用管que.push({newX, newY});}}}vector<vector<char> > surroundedArea(vector<vector<char> >& board) {// write code hereint m = board.size(), n = board[0].size();for (int i = 0; i < m; i++) {if (board[i][0] == 'O') bfs(board, i, 0);if (board[i][n - 1] == 'O') bfs(board, i, n - 1);}for (int j = 0; j < n; j++) {if (board[0][j] == 'O') bfs(board, 0, j);if (board[m - 1][j] == 'O') bfs(board, m - 1, j);}for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (board[i][j] == 'E') board[i][j] = 'O';else board[i][j] = 'X';}}return board;}
};