题目1:【二刷完成】
代码:
以下代码超时
class Solution {
private LinkedList<String> res;
private LinkedList<String> path = new LinkedList<>();
public List<String> findItinerary(List<List<String>> tickets) {
Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
path.add("JFK");
boolean[] used = new boolean[tickets.size()];
backTracking((ArrayList) tickets, used);
return res;
}
public boolean backTracking(ArrayList<List<String>> tickets, boolean[] used) {
if (path.size()==tickets.size()+1){
res = new LinkedList(path);
return true;
}
for(int i=0;i<tickets.size();i++){
if (!used[i] && tickets.get(i).get(0).equals(path.getLast())){
path.add(tickets.get(i).get(1));
used[i] = true;
if (backTracking(tickets,used)){
return true;
}
//backTracking(tickets,used);
used[i] = false;
path.removeLast();
}
}
return false;
}
}
采用hashmap和priorityQueue
class Solution {
private Map<String, PriorityQueue<String>> map = new HashMap<>();
private LinkedList<String> result = new LinkedList<>();
public List<String> findItinerary(List<List<String>> tickets) {
for(List<String> ticket : tickets){
String src = ticket.get(0);
String dst = ticket.get(1);
if (!map.containsKey(src)){
PriorityQueue<String> pq = new PriorityQueue<>();
map.put(src,pq);
}
map.get(src).add(dst);
}
dfs("JFK");
return result;
}
public void dfs(String src){
PriorityQueue<String> pq = map.get(src);
while(pq!=null && !pq.isEmpty()){
dfs(pq.poll());
}
result.addFirst(src);
}
}
题目2:【二刷完成】
class Solution {
List<List<String>> result = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
char[][] chessBord = new char[n][n];
for (char[] c : chessBord){
Arrays.fill(c,'.');
}
backTracking(n,0,chessBord);
return result;
}
void backTracking(int n, int row, char[][] chessBord){
if (row==n){
result.add(Array2List(chessBord));
return;
}
for(int col=0;col<n;col++){
if (isValid(row,col,chessBord,n)){
chessBord[row][col]='Q';
backTracking(n,row+1,chessBord);
chessBord[row][col]='.';
}
}
}
boolean isValid(int row, int col,char[][] chessBord, int n){
for (int i=0;i<row;i++){
if (chessBord[i][col]=='Q') return false;
}
for (int i=0;i<col;i++){
if (chessBord[row][col]=='Q') return false;
}
for (int i=row-1,j=col-1;i>=0 && j>=0;i--,j--){
if (chessBord[i][j]=='Q') return false;
}
for (int i=row-1,j=col+1;i>=0 && j<n;i--,j++){
if (chessBord[i][j]=='Q') return false;
}
return true;
}
public List Array2List(char[][] chessBord) {
List<String> list = new ArrayList<>();
for (char[] c : chessBord) {
list.add(String.copyValueOf(c));
}
return list;
}
}
题目3:【二刷完成】
class Solution {
public void solveSudoku(char[][] board) {
backTracking(board);
}
boolean backTracking(char[][] board){
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
if (board[i][j]!='.') continue;
for (char k='1';k<='9';k++){
if (isValid(i,j,k,board)){
board[i][j]=k;
if (backTracking(board)){
return true;
}
}
board[i][j]='.';
}
return false;
}
}
return true;
}
boolean isValid(int row, int col, int value, char[][] board){
for (int i=0;i<9;i++){
if (board[row][i]==value) return false;
}
for (int i=0;i<9;i++){
if (board[i][col]==value) return false;
}
int startRow = (row/3)*3;
int startCol = (col/3)*3;
for (int i=startRow;i<startRow+3;i++){
for (int j=startCol;j<startCol+3;j++){
if (board[i][j]==value) return false;
}
}
return true;
}
}