332.重新安排行程
题目链接:332. 重新安排行程 - 力扣(LeetCode)
时间超出限制的代码:
class Solution {
List<List<String>> lists = new ArrayList<>();
public List<String> findItinerary(List<List<String>> tickets) {
Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
backtrack(tickets, "JFK", new boolean[tickets.size()], new ArrayList<>());
lists.getFirst().addFirst("JFK");
return lists.getFirst();
}
private void backtrack(List<List<String>> tickets, String src, boolean[] used, List<String> list) {
if(!lists.isEmpty()) {
return;
}
if(tickets.size() == list.size()) {
lists.add(new ArrayList<>(list));
return;
}
for(int i = 0; i < tickets.size(); ++i) {
if(used[i]) {
continue;
}
if(tickets.get(i).getFirst().equals(src)) {
list.add(tickets.get(i).getLast());
used[i] = true;
backtrack(tickets, tickets.get(i).getLast(), used, list);
list.removeLast();
used[i] = false;
}
}
}
}
carl代码(也超出时间限制):
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;
}
used[i] = false;
path.removeLast();
}
}
return false;
}
}
欧拉回路:
class Solution {
Map<String, PriorityQueue<String>> map = new HashMap<String, PriorityQueue<String>>();
List<String> itinerary = new LinkedList<String>();
public List<String> findItinerary(List<List<String>> tickets) {
for (List<String> ticket : tickets) {
String src = ticket.get(0), dst = ticket.get(1);
if (!map.containsKey(src)) {
map.put(src, new PriorityQueue<String>());
}
map.get(src).offer(dst);
}
dfs("JFK");
//Collections.reverse(itinerary);
return itinerary;
}
public void dfs(String curr) {
while (map.containsKey(curr) && map.get(curr).size() > 0) {
String tmp = map.get(curr).poll();
dfs(tmp);
}
itinerary.addFirst(curr);
}
}
51. N皇后
class Solution {
List<List<String>> ans = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
int[][] cb = new int[n][n];
backtrack(n, 0, cb);
return ans;
}
private void backtrack(int n, int index, int[][] cb) {
if(index == n) {
ans.add(cbtolist(cb));
return;
}
for(int i = 0; i < n; ++i) {
if(cb[index][i] != 0) {
continue;
}else {
change(index, i, n, cb);
backtrack(n, index + 1, cb);
rechange(index, i, n, cb);
}
}
}
private void change(int index, int i, int n, int[][] cb) {
for(int j = 0; j < n; ++j) {
cb[j][i] += (index+1);
cb[index][j] += (index+1);
}
for(int j = 0,k = 0; j + index < n && k + i < n; ++j,++k) {
cb[j+index][k+i] += (index+1);
}
for(int j = -1, k = -1; j + index >=0 && k + i >=0; --j,--k) {
cb[j+index][k+i] += (index+1);
}
for(int j = -1,k= 1; j + index >= 0 && k + i < n; --j, ++k) {
cb[j+index][k+i] += (index+1);
}
for(int j = 1,k = -1; j + index < n && k + i >= 0; ++j,--k) {
cb[j+index][k+i] += (index+1);
}
cb[index][i] = 100;
}
private void rechange(int index, int i, int n, int[][] cb) {
for(int j = 0; j < n; ++j) {
cb[j][i] -= (index+1);
cb[index][j] -= (index+1);
}
for(int j = 0,k = 0; j + index < n && k + i < n; ++j,++k) {
cb[j+index][k+i] -= (index+1);
}
for(int j = -1, k = -1; j + index >=0 && k + i >=0; --j,--k) {
cb[j+index][k+i] -= (index+1);
}
for(int j = -1,k= 1; j + index >= 0 && k + i < n; --j, ++k) {
cb[j+index][k+i] -= (index+1);
}
for(int j = 1,k = -1; j + index < n && k + i >= 0; ++j,--k) {
cb[j+index][k+i] -= (index+1);
}
cb[index][i] = 0;
}
private List<String> cbtolist(int[][] cb) {
List<String> ans = new ArrayList<>();
for(int i = 0; i < cb.length; ++i) {
StringBuffer a = new StringBuffer();
for(int j = 0; j < cb.length; ++j) {
if(cb[i][j] == 100) {
a.append('Q');
}else {
a.append('.');
}
}
ans.addLast(a.toString());
}
return ans;
}
}
37. 解数独
写回溯写麻了,这题先放着