add
This commit is contained in:
parent
5770a9f011
commit
121cc9b5ba
35
5.leetcode/src/com/fanxb/common/Q130.java
Normal file
35
5.leetcode/src/com/fanxb/common/Q130.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class Q130 {
|
||||
public void solve(char[][] board) {
|
||||
int m = board.length, n = board[0].length;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
boolean isBoard = i == 0 || i == m - 1 || j == 0 || j == n - 1;
|
||||
if (isBoard && board[i][j] == 'O') {
|
||||
dfs(board, i, j, m, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (board[i][j] == 'O') board[i][j] = 'X';
|
||||
if(board[i][j]=='0') board[i][j]='O';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有边上的
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private void dfs(char[][] board, int i, int j, int m, int n) {
|
||||
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] == 'X' || board[i][j] == '0') return;
|
||||
board[i][j] = '0';
|
||||
dfs(board, i + 1, j, m, n);
|
||||
dfs(board, i - 1, j, m, n);
|
||||
dfs(board, i, j + 1, m, n);
|
||||
dfs(board, i, j - 1, m, n);
|
||||
}
|
||||
}
|
67
5.leetcode/src/com/fanxb/common/Q133.java
Normal file
67
5.leetcode/src/com/fanxb/common/Q133.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q133 {
|
||||
public static class Node {
|
||||
public int val;
|
||||
public List<Node> neighbors;
|
||||
|
||||
public Node() {
|
||||
val = 0;
|
||||
neighbors = new ArrayList<Node>();
|
||||
}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
neighbors = new ArrayList<Node>();
|
||||
}
|
||||
|
||||
public Node(int _val, ArrayList<Node> _neighbors) {
|
||||
val = _val;
|
||||
neighbors = _neighbors;
|
||||
}
|
||||
}
|
||||
|
||||
public Node cloneGraph(Node node) {
|
||||
Map<Node, Node> map = new HashMap<>();
|
||||
return dfs(node, map);
|
||||
}
|
||||
|
||||
private Node dfs(Node node, Map<Node, Node> map) {
|
||||
if (node == null) return null;
|
||||
Node temp = map.get(node);
|
||||
if (temp != null) return temp;
|
||||
temp = new Node(node.val, new ArrayList<>());
|
||||
map.put(node, temp);
|
||||
for (Node n : node.neighbors) {
|
||||
Node ng = dfs(n, map);
|
||||
temp.neighbors.add(ng);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
public Node cloneGraph1(Node node) {
|
||||
if (node == null) return null;
|
||||
Map<Node, Node> map = new HashMap<>();
|
||||
Node res = new Node(node.val, new ArrayList<>());
|
||||
map.put(node, res);
|
||||
Queue<Node> queue = new LinkedList<>();
|
||||
queue.offer(node);
|
||||
while (!queue.isEmpty()) {
|
||||
Node temp = queue.poll();
|
||||
List<Node> list = map.get(temp).neighbors;
|
||||
for (Node n : temp.neighbors) {
|
||||
Node nc = map.get(n);
|
||||
if (nc == null) {
|
||||
queue.add(n);
|
||||
nc = new Node(n.val, new ArrayList<>());
|
||||
map.put(n, nc);
|
||||
}
|
||||
list.add(nc);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/Q200.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q200.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class Q200 {
|
||||
public int numIslands(char[][] grid) {
|
||||
int m = grid.length, n = grid[0].length, res = 0;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (grid[i][j] == '1') {
|
||||
dfs(grid, i, j, m, n);
|
||||
res++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(char[][] grid, int i, int j, int m, int n) {
|
||||
if (i < 0 || i >= m || j < 0 || j >= n) return;
|
||||
if (grid[i][j] == '0' || grid[i][j] == '2') return;
|
||||
//访问过的陆地
|
||||
grid[i][j] = '2';
|
||||
dfs(grid, i, j + 1, m, n);
|
||||
dfs(grid, i, j - 1, m, n);
|
||||
dfs(grid, i - 1, j, m, n);
|
||||
dfs(grid, i + 1, j, m, n);
|
||||
}
|
||||
}
|
37
5.leetcode/src/com/fanxb/common/Q207.java
Normal file
37
5.leetcode/src/com/fanxb/common/Q207.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q207 {
|
||||
public boolean canFinish(int numCourses, int[][] prerequisites) {
|
||||
//记录课程的入度
|
||||
int[] inCount = new int[numCourses];
|
||||
//记录课程的后续课程
|
||||
Map<Integer, List<Integer>> nextCourses = new HashMap<>(numCourses);
|
||||
for (int[] pre : prerequisites) {
|
||||
inCount[pre[0]]++;
|
||||
List<Integer> next = nextCourses.computeIfAbsent(pre[1], k -> new ArrayList<>());
|
||||
next.add(pre[0]);
|
||||
}
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
if (inCount[i] == 0) queue.offer(i);
|
||||
}
|
||||
int count = 0;
|
||||
while (!queue.isEmpty()) {
|
||||
int c = queue.poll();
|
||||
count++;
|
||||
List<Integer> next = nextCourses.get(c);
|
||||
if (next == null) continue;
|
||||
next.forEach(item -> {
|
||||
inCount[item]--;
|
||||
if (inCount[item] == 0) queue.offer(item);
|
||||
});
|
||||
}
|
||||
return numCourses == count;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Q207().canFinish(2, new int[][]{{0, 1}});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user