LeetCode刷题 Day30
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.
Example 1:
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
Output: ["JFK","MUC","LHR","SFO","SJC"]
Example 2:
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
思路:
- 将ticket排序,保证smaller lexical order
- 通过used cache判断纵向每个位置是否使用过
代码:
var findItinerary = function(tickets) {
let res = [];
let path = ['JFK'];
let sortArray = function(a,b) {
if(a[0] == b[0]) return (a[1] < b[1] ? -1: (a[1] > b[1] ? 1: 0));
return (a[0] < b[0] ? -1 : 1);
}
tickets.sort(sortArray)
let helper = function(path, used) {
if (path.length === tickets.length + 1) {
res = path;
return true;
}
for (let i = 0; i < tickets.length; i++) {
if (!used[i] && path[path.length - 1] === tickets[i][0]) {
path.push(tickets[i][1]);
used[i] = true;
if (helper(path, used)) {
return true;
}
used[i] = false;
path.pop();
}
}
}
helper(path, {});
return res;
};
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.
Example 1:
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
Example 2:
Input: n = 1
Output: [["Q"]]
思路:
- 判断是否有效,同一行,一列,左上45度,右上45度
- 以递归深度为row, for loop为col 代码:
var solveNQueens = function(n) {
let board = [];
let res = [];
for (let i = 0; i < n; i++) {
board.push(Array(n).fill('.'));
}
let helper = function(row) {
if (row >= n) {
console.log(board);
const temp = board.map(item => item.join(''));
res.push(temp);
return;
}
for (let col = 0; col < n; col++) {
if (isValid(board, row, col)) {
board[row][col] = 'Q';
helper(row + 1);
board[row][col] = '.';
}
}
}
let isValid = function(board, row, col) {
//row
for (let i = 0; i < n; i++) {
if (board[i][col] === 'Q') return false;
}
//col
for (let i = 0; i < n; i++) {
if (board[row][i] === 'Q') return false;
}
//up -45
for (let i = row, j = col; i >= 0 && j >= 0; i--,j--) {
if (board[i][j] === 'Q') return false;
}
//up 45
for (let i = row, j = col; i >= 0 && j < n; i--,j++) {
if (board[i][j] === 'Q') return false;
}
return true;
}
helper(0);
return res;
}
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.
Example 1:
Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
Explanation: The input board is shown above and the only valid solution is shown below:
思路:
- 因为是唯一解,所以当pass validation之后就应该及时返回
- 遍历完1-9的数字没有合适的解,说明要返回到上一层重来
- 遍历到末尾还没有return false说明符合条件
代码:
var solveSudoku = function(board) {
let res = [];
let helper = function() {
for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board.length; col++) {
if (board[row][col] !== '.') continue;
for (let val = 1; val <= 9; val++) {
if (isValid(row, col, `${val}`)) {
board[row][col] = `${val}`;
if(helper()) return true;
board[row][col] = '.';
}
}
return false;
}
}
return true;
}
let isValid = function(row, col, val) {
for (let i = 0; i < board.length; i++) {
if (board[i][col] === val) return false;
}
for (let i = 0; i < board.length; i++) {
if (board[row][i] === val) return false;
}
let startRow = Math.floor(row / 3) * 3;
let startCol = Math.floor(col / 3) * 3;
for (let i = startRow; i < startRow + 3; i++) {
for (let j = startCol; j < startCol + 3; j++) {
if (board[i][j] === val) return false;
}
}
return true;
}
helper();
return res;
};