From 420432bf932d23bb3a61bea8024933e451175ecf Mon Sep 17 00:00:00 2001 From: Xu Jian Date: Sun, 22 Dec 2019 23:26:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E4=BA=A4=E7=AC=AC=E4=BA=94=E7=AC=AC?= =?UTF-8?q?=E5=85=AD=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week 05/id_666/LeetCode_62_666.java | 13 ++++++ Week 05/id_666/LeetCode_70_666.java | 16 +++++++ Week 06/id_666/LeetCode_200_666.java | 35 +++++++++++++++ Week 06/id_666/LeetCode_51_666.java | 66 ++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 Week 05/id_666/LeetCode_62_666.java create mode 100644 Week 05/id_666/LeetCode_70_666.java create mode 100644 Week 06/id_666/LeetCode_200_666.java create mode 100644 Week 06/id_666/LeetCode_51_666.java diff --git a/Week 05/id_666/LeetCode_62_666.java b/Week 05/id_666/LeetCode_62_666.java new file mode 100644 index 000000000..fb9f95938 --- /dev/null +++ b/Week 05/id_666/LeetCode_62_666.java @@ -0,0 +1,13 @@ +class Solution { + public int uniquePaths(int m, int n) { + //典型动态规化 + int[] cur = new int[n]; + Arrays.fill(cur,1); + for (int i = 1; i < m;i++){ + for (int j = 1; j < n; j++){ + cur[j] += cur[j-1] ; + } + } + return cur[n-1]; + } +} \ No newline at end of file diff --git a/Week 05/id_666/LeetCode_70_666.java b/Week 05/id_666/LeetCode_70_666.java new file mode 100644 index 000000000..f9166848b --- /dev/null +++ b/Week 05/id_666/LeetCode_70_666.java @@ -0,0 +1,16 @@ +class Solution { + public int climbStairs(int n) { + //递归思路,f(n) = f(n-1) + f(n-2); + //动态规划解法 + if (n == 1) { + return 1; + } + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } +} \ No newline at end of file diff --git a/Week 06/id_666/LeetCode_200_666.java b/Week 06/id_666/LeetCode_200_666.java new file mode 100644 index 000000000..7c2801fd2 --- /dev/null +++ b/Week 06/id_666/LeetCode_200_666.java @@ -0,0 +1,35 @@ +class Solution { + //没整明白 + public int numIslands(char[][] grid) { + if (grid == null || grid.length == 0) { + return 0; + } + int nr = grid.length; + int nc = grid[0].length; + int num_islands = 0; + for (int r = 0; r < nr; ++r) { + for (int c = 0; c < nc; ++c) { + if (grid[r][c] == '1') { + ++num_islands; + dfs(grid, r, c); + } + } + } + return num_islands; + } + + private void dfs(char[][] grid, int r, int c) { + int nr = grid.length; + int nc = grid[0].length; + + if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') { + return; + } + + grid[r][c] = '0'; + dfs(grid, r - 1, c); + dfs(grid, r + 1, c); + dfs(grid, r, c - 1); + dfs(grid, r, c + 1); + } +} \ No newline at end of file diff --git a/Week 06/id_666/LeetCode_51_666.java b/Week 06/id_666/LeetCode_51_666.java new file mode 100644 index 000000000..bd71c731e --- /dev/null +++ b/Week 06/id_666/LeetCode_51_666.java @@ -0,0 +1,66 @@ +class Solution { + //背诵题 + int rows[]; + int hills[]; + int dales[]; + int n; + List> output = new ArrayList(); + int queens[]; + + public boolean isNotUnderAttack(int row, int col) { + int res = rows[col] + hills[row - col + 2 * n] + dales[row + col]; + return (res == 0) ? true : false; + } + + public void placeQueen(int row, int col) { + queens[row] = col; + rows[col] = 1; + hills[row - col + 2 * n] = 1; // "hill" diagonals + dales[row + col] = 1; //"dale" diagonals + } + + public void removeQueen(int row, int col) { + queens[row] = 0; + rows[col] = 0; + hills[row - col + 2 * n] = 0; + dales[row + col] = 0; + } + + public void addSolution() { + List solution = new ArrayList(); + for (int i = 0; i < n; ++i) { + int col = queens[i]; + StringBuilder sb = new StringBuilder(); + for(int j = 0; j < col; ++j) sb.append("."); + sb.append("Q"); + for(int j = 0; j < n - col - 1; ++j) sb.append("."); + solution.add(sb.toString()); + } + output.add(solution); + } + + public void backtrack(int row) { + for (int col = 0; col < n; col++) { + if (isNotUnderAttack(row, col)) { + placeQueen(row, col); + // if n queens are already placed + if (row + 1 == n) addSolution(); + // if not proceed to place the rest + else backtrack(row + 1); + // backtrack + removeQueen(row, col); + } + } + } + + public List> solveNQueens(int n) { + this.n = n; + rows = new int[n]; + hills = new int[4 * n - 1]; + dales = new int[2 * n - 1]; + queens = new int[n]; + + backtrack(0); + return output; + } +} \ No newline at end of file