package top100.递归回溯;
import java.util.*;
public class TOP {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
LinkedList<Integer> path = new LinkedList<>();
boolean[] visited = new boolean[nums.length];
dfs(nums, visited, path);
return res;
}
private void dfs(int[] nums, boolean[] visited, LinkedList<Integer> path) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
} else if (path.size() < nums.length) {
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
visited[i] = true;
path.add(nums[i]);
dfs(nums, visited, path);
path.removeLast();
visited[i] = false;
}
}
}
}
List<List<Integer>> lists = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
List<Integer> path = new ArrayList<>();
backtrack(nums, 0, path);
return lists;
}
private void backtrack(int[] nums, int start, List<Integer> path) {
lists.add(new ArrayList<>(path));
for (int i = start; i < nums.length; i++) {
path.add(nums[i]);
backtrack(nums, i + 1, path);
path.remove(path.size() - 1);
}
}
List<String> res = new ArrayList<>();
private final String[] MAP = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
if ("".equals(digits)) return res;
char[] path = new char[digits.length()];
dfs(digits, 0, path);
return res;
}
private void dfs(String digits, int index, char[] path) {
if (index == digits.length()) {
res.add(new String(path));
} else {
for (Character c : MAP[digits.charAt(index) - '0'].toCharArray()) {
path[index] = c;
dfs(digits, index + 1, path);
}
}
}
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
LinkedList<Integer> path = new LinkedList<>();
dfs(candidates, target, 0, path);
return res;
}
private void dfs(int[] candidates, int target, int begin, LinkedList<Integer> path) {
if (target == 0) {
res.add(new ArrayList<>(path));
return;
}
for (int i = begin; i < candidates.length; i++) {
if (target - candidates[i] < 0) {
break;
}
path.add(candidates[i]);
dfs(candidates, target - candidates[i], i, path);
path.removeLast();
}
}
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
StringBuilder path = new StringBuilder();
dfs(n, 0, 0, path);
return res;
}
private void dfs(int n, int left, int right, StringBuilder path) {
if (left == n && right == n) {
res.add(new String(path));
return;
}
if (left < n) {
path.append('(');
dfs(n, left + 1, right, path);
path.deleteCharAt(path.length() - 1);
}
if (right < left && right < n) {
path.append(')');
dfs(n, left, right + 1, path);
path.deleteCharAt(path.length() - 1);
}
}
int[][] ways = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
boolean[][] visited;
int m, n;
public boolean exist(char[][] board, String word) {
m = board.length;
n = board[0].length;
visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = true;
if (board[i][j] == word.charAt(0) && dfs(board, word, 1, i, j)) {
return true;
}
visited[i][j] = false;
}
}
return false;
}
private boolean dfs(char[][] board, String word, int index, int i, int j) {
if (index == word.length()) {
return true;
}
for (int[] way : ways) {
int x = i + way[0], y = j + way[1];
if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && board[x][y] == word.charAt(index)) {
visited[x][y] = true;
if (dfs(board, word, index + 1, x, y)) return true;
visited[x][y] = false;
}
}
return false;
}
List<List<String>> lists = new ArrayList<>();
Deque<String> deque = new LinkedList<>();
public List<List<String>> partition(String s) {
backTracking(s, 0);
return lists;
}
private void backTracking(String s, int startIndex) {
if (startIndex >= s.length()) {
lists.add(new ArrayList(deque));
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isPalindrome(s, startIndex, i)) {
String str = s.substring(startIndex, i + 1);
deque.addLast(str);
backTracking(s, i + 1);
deque.removeLast();
}
}
}
private boolean isPalindrome(String s, int startIndex, int end) {
for (int i = startIndex, j = end; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}