代码随想录算法训练营第二十六天-332重新安排行程、51N皇后、 37解数独
1 2
| 24-06-12 初始记录 24-06-13 完成题解
|
初步题解
332 重新安排行程
题目链接:(https://leetcode.cn/problems/reconstruct-itinerary)
51N 皇后
题目链接:(https://leetcode.cn/problems/n-queens)
37 解数独
题目链接:(https://leetcode.cn/problems/sudoku-solver)
看解析
332 重新安排行程
题目链接/文章讲解:(https://programmercarl.com/0332.重新安排行程.html)
解法超时(第 80 个用例):加上剪枝不会超时。别的思路都和清晰,但是剪枝那一步有点难想到。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public static List<String> findItinerary(List<List<String>> tickets) { List<String> resultOne = new ArrayList<>(); tickets.sort(Comparator.comparing(o -> o.get(1))); boolean[] flagList = new boolean[tickets.size()]; resultOne.add("JFK"); findItineraryDFS(flagList, tickets, resultOne); return resultOne; } private static boolean findItineraryDFS(boolean[] flagList, List<List<String>> tickets, List<String> resultOne) { if (resultOne.size() == tickets.size() + 1) { return true; } for (int i = 0; i < tickets.size(); i++) { if(i > 0 && tickets.get(i).equals(tickets.get(i - 1)) && !flagList[i - 1]) continue; if (!flagList[i] && tickets.get(i).get(0).equals(resultOne.get(resultOne.size() - 1))) { resultOne.add(tickets.get(i).get(1)); flagList[i] = true; if (findItineraryDFS(flagList, tickets, resultOne)) { return true; } resultOne.remove(resultOne.size() - 1); flagList[i] = false; } } return false; }
|
51N 皇后
题目链接/文章讲解:(https://programmercarl.com/0051.N皇后.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| public class LE51 { public static void main(String[] args) { solveNQueens(4); } public static List<List<String>> solveNQueens(int n) { List<List<String>> result = new ArrayList<>(); char[][] chars = new char[n][n]; for (char[] c : chars) { Arrays.fill(c, '.'); } solveNQueensDFS(0, n, chars, result); return result; }
private static void solveNQueensDFS(int row, int n, char[][] chars, List<List<String>> result) { if (row == n) { List<String> list = new ArrayList<>(); for (char[] aChar : chars) { list.add(String.copyValueOf(aChar)); } result.add(list); return; } for (int i = 0; i < n; i++) { if (isValid(row, i, n, chars)) { chars[row][i] = 'Q'; solveNQueensDFS(row + 1, n, chars, result); chars[row][i] = '.'; } } } private static boolean isValid(int row, int col, int n, char[][] chars) { for (int i = 0; i < row; ++i) { if (chars[i][col] == 'Q') { return false; } } for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (chars[i][j] == 'Q') { return false; } } for (int i = row - 1, j = col + 1; i >= 0 && j <= n - 1; i--, j++) { if (chars[i][j] == 'Q') { return false; } } return true; } }
|
37 解数独
题目链接/文章讲解:(https://programmercarl.com/0037.解数独.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| public class LE37 { public void solveSudoku(char[][] board) { solveSudokuDFS(board); } private boolean solveSudokuDFS(char[][] board) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (board[i][j] != '0') { continue; } for (char k = '1'; k <= '9'; k++) { if (isvalid(i, j, k, board)) { board[i][j] = k; if (solveSudokuDFS(board)) { return true; } board[i][j] = '.'; } } return false; } } return true; }
private boolean isvalid(int i, int j, char k, char[][] board) { for (int index = 0; index < board.length; index++) { if (board[i][index] == k) { return false; } if (board[index][j] == k) { return false; } } int x = (i / 3) * 3; int y = (j / 3) * 3; for (int indexX = x; indexX < x + 3; indexX++) { for (int indexY = y; indexY < y + 3; indexY++) { if (board[indexX][indexY] == k) { return false; } } } return true; } }
|