diff --git a/5.leetcode/src/com/fanxb/common/Q130.java b/5.leetcode/src/com/fanxb/common/Q130.java new file mode 100644 index 0000000..16a765d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q130.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q133.java b/5.leetcode/src/com/fanxb/common/Q133.java new file mode 100644 index 0000000..8deccdb --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q133.java @@ -0,0 +1,67 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q133 { + public static class Node { + public int val; + public List neighbors; + + public Node() { + val = 0; + neighbors = new ArrayList(); + } + + public Node(int _val) { + val = _val; + neighbors = new ArrayList(); + } + + public Node(int _val, ArrayList _neighbors) { + val = _val; + neighbors = _neighbors; + } + } + + public Node cloneGraph(Node node) { + Map map = new HashMap<>(); + return dfs(node, map); + } + + private Node dfs(Node node, Map 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 map = new HashMap<>(); + Node res = new Node(node.val, new ArrayList<>()); + map.put(node, res); + Queue queue = new LinkedList<>(); + queue.offer(node); + while (!queue.isEmpty()) { + Node temp = queue.poll(); + List 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q200.java b/5.leetcode/src/com/fanxb/common/Q200.java new file mode 100644 index 0000000..d2385ee --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q200.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q207.java b/5.leetcode/src/com/fanxb/common/Q207.java new file mode 100644 index 0000000..25c5914 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q207.java @@ -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> nextCourses = new HashMap<>(numCourses); + for (int[] pre : prerequisites) { + inCount[pre[0]]++; + List next = nextCourses.computeIfAbsent(pre[1], k -> new ArrayList<>()); + next.add(pre[0]); + } + Queue 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 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}}); + } +}