搜索
Posted 保护眼睛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搜索相关的知识,希望对你有一定的参考价值。
130.被围绕的区域
class Solution130 {
int[][] nextP = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public void dfs(char[][] board, int row, int col, int x, int y) {
board[x][y] = 'A';
for (int i = 0; i < 4; i++) {
int nextX = x + nextP[i][0];BFS
int nextY = y + nextP[i][1];
if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col)
continue;
if (board[nextX][nextY] == 'O')
dfs(board, row, col, nextX, nextY);
}
}
public void solve(char[][] board) {
int row = board.length;
int col = board[0].length;
for (int i = 0; i < col; i++) {
if (board[0][i] == 'O')
dfs(board, row, col, 0, i);
if (board[row - 1][i] == 'O')
dfs(board, row, col, row - 1, i);
}
for (int j = 1; j < row - 1; j++) {
if (board[j][0] == 'O')
dfs(board, row, col, j, 0);
if (board[j][col - 1] == 'O')
dfs(board, row, col, j, col - 1);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == 'A') {
board[i][j] = 'O';
} else {
continue;
}
}
}
}
}
200.岛屿数量
class Solution200 {
int[][] nextP = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public void dfs(char[][] grid, int row, int col, boolean[][] visited, int x, int y) {
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
int nextX = x + nextP[i][0];
int nextY = y + nextP[i][1];
if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col)
continue;
if (grid[nextX][nextY] == '1' && !visited[nextX][nextY]) {
dfs(grid, row, col, visited, nextX, nextY);
}
}
}
public int numIslands(char[][] grid) {
int res = 0;
int row = grid.length;
int col = grid[0].length;
boolean[][] visited = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == '1' && !visited[i][j]) {
res++;
dfs(grid, row, col, visited, i, j);
}
}
}
return res;
}
}
690.员工的重要性
class Solution690 {
//bfs
public int getImportance(List<Employee> employees, int id) {
HashMap<Integer, Employee> hashMap = new HashMap<>();
for (int i = 0; i < employees.size(); i++) {
hashMap.put(employees.get(i).id, employees.get(i));
}
Queue<Integer> queue = new LinkedList<>();
queue.offer(id);
int res = 0;
while (!queue.isEmpty()) {
int curId = queue.poll();
res += hashMap.get(curId).importance;
int size = hashMap.get(curId).subordinates.size();
for (int i = 0; i < size; i++) {
queue.offer(hashMap.get(curId).subordinates.get(i));
}
}
return res;
}
}
429.N叉树的层序遍历
class Solution429 {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null)
return res;
Queue<Node> treeQ = new LinkedList<>();
treeQ.offer(root);
while (!treeQ.isEmpty()) {
int size = treeQ.size();
List<Integer> tmp = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node cur = treeQ.poll();
if (cur != null) {
tmp.add(cur.val);
int childSize = cur.children.size();
for (int j = 0; j < childSize; j++) {
treeQ.offer(cur.children.get(j));
}
}
}
res.add(tmp);
}
return res;
}
}
994.腐烂的橘子
class Solution994 {
int[][] nextP = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public int orangesRotting(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
Queue<HashMap<Integer, Integer>> queue = new LinkedList<>();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 2) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
hashMap.put(i, j);
queue.offer(hashMap);
}
}
}
int minTime = 0;
while (!queue.isEmpty()) {
int size = queue.size();
boolean flag = true;
while (size-- > 0) {
HashMap<Integer, Integer> tmp = queue.poll();
Iterator iter = tmp.entrySet().iterator();
Integer x = -1;
Integer y = -1;
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
x = (Integer) entry.getKey();
y = (Integer) entry.getValue();
}
for (int i = 0; i < 4; i++) {
int nextX = x + nextP[i][0];
int nextY = y + nextP[i][1];
if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col)
continue;
if (grid[nextX][nextY] == 1) {
flag = false;
grid[nextX][nextY] = 2;
HashMap<Integer, Integer> map = new HashMap<>();
map.put(nextX, nextY);
queue.offer(map);
}
}
}
if (!flag)
minTime++;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 1)
return -1;
}
}
return minTime;
}
}
以上是关于搜索的主要内容,如果未能解决你的问题,请参考以下文章
《安富莱嵌入式周报》第279期:强劲的代码片段搜索工具,卡内基梅隆大学安全可靠C编码标准,Nordic发布双频WiFi6 nRF7002芯片