332. Reconstruct Itinerary
You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
- For example, the itinerary
["JFK", "LGA"]has a smaller lexical order than["JFK", "LGB"].
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
题目解析:
- 需要找到按字母排序最小的路径,可以先进行排序,第一条出现的路径为目标路径
代码:
class Solution {
List<String> result = null;
public List<String> findItinerary(List<List<String>> tickets) {
Collections.sort(tickets, (a, b) -> {
if (a.get(0).equals(b.get(0))) {
return a.get(1).compareTo(b.get(1));
}
return a.get(0).compareTo(b.get(0)) ;
});
boolean[] used = new boolean[tickets.size()];
backtracking(tickets, new ArrayList<>(Arrays.asList("JFK")), used);
return result;
}
public void backtracking(List<List<String>> tickets, List<String> path, boolean[] used) {
if (result != null) return;
if (path.size() == tickets.size() + 1) {
result = new ArrayList<>(path);
return;
}
for (int i = 0; i< tickets.size(); i++) {
if (used[i] || !tickets.get(i).get(0).equals(path.get(path.size() -1))) continue;
used[i] = true;
path.add(tickets.get(i).get(1));
backtracking(tickets, path, used);
path.remove(path.size() - 1);
used[i] = false;
}
}
}
51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.
题目解析:
- 因为是按行放置,所以判断当前文职是否合法,需要判断三个方向:上,左上 和 右上。
- 使用回溯遍历所有情况
代码:
lass Solution {
List<List<String>> result = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '.';
}
}
backtracking(n, 0, board);
return result;
}
public void backtracking(int n, int row, char[][] board) {
if (row == n) {
List<String> solution = new ArrayList<>();
for (int i = 0; i < n; i++) {
solution.add(new String(board[i]));
}
result.add(solution);
return;
}
for (int i = 0; i < n; i++) {
if (isValid(row, i, board)) {
board[row][i] = 'Q';
backtracking(n, row + 1, board);
board[row][i] = '.';
}
}
}
public boolean isValid(int row, int col, char[][] board) {
int n = board.length;
for (int i = row - 1; i >= 0; i--) {
if (board[i][col] == 'Q') return false;
}
for (int i = row - 1, j = col - 1; i >= 0 && j>= 0; i--, j--) {
if (board[i][j] == 'Q') return false;
}
for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (board[i][j] == 'Q') return false;
}
return true;
}
}
37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9must occur exactly once in each row. - Each of the digits
1-9must occur exactly once in each column. - Each of the digits
1-9must occur exactly once in each of the 93x3sub-boxes of the grid.
The '.' character indicates empty cells.
题目解析:
- 一个一个遍历,如果没有所有数都不满足当前位置,回溯
代码:
class Solution {
public void solveSudoku(char[][] board) {
backtracking(0, 0, board);
}
public boolean backtracking(int row, int col, char[][] board) {
int n = board.length;
if (col >= n) {
col %= n;
row++;
if (row >= n) return true;;
}
if (board[row][col] != '.') return backtracking(row, col + 1, board);
boolean flag = false;
for (int i = 1; i <= 9; i++) {
char val = String.valueOf(i).charAt(0);
if (isValid(val, row, col, board)) {
board[row][col] = val;
if (backtracking(row, col + 1, board)){
return true;
}
board[row][col] = '.';
}
}
return flag;
}
public boolean isValid(char val, int row, int col, char[][] board) {
for (int i = 0; i < board.length; i++) {
if (board[row][i] == val || board[i][col] == val) return false;
}
int boxRow = row / 3, boxCol = col /3;
for (int i = boxRow * 3; i < (boxRow + 1) * 3; i++) {
for (int j = boxCol * 3; j < (boxCol + 1) * 3; j++) {
if (i == row && j == col) continue;
if (board[i][j] == val) return false;
}
}
return true;
}
}