// start means the start of the substring voiddfs(int i, int start) { // [start, i] if (i == s.length()) { ans.add(newArrayList<String>(path)); return ; } // not choose the comma between i and i + 1 if (i < s.length() - 1) { // [start, i + 1] dfs(i + 1, start); } // choose the comma that only the method isPalindrome returned true between i and i + 1 if (isPalindrome(start, i)) { // [start, i] path.push(s.substring(start, i + 1)); // [i + 1, i + 1] dfs(i + 1, i + 1); // restore the scene path.pop(); } }
booleanisPalindrome(int left, int right) { while (left < right) { if (s.charAt(left++) != s.charAt(right--)) { returnfalse; } } returntrue; } } // aabb // [["aa","bb"],["aa","b","b"],["a","a","bb"],["a","a","b","b"]]
classSolution { publicintmaximumRows(int[][] mat, int cols) { intm= mat.length, n = mat[0].length; int[] mask = newint[m]; for (inti=0; i < m; ++i) { for (intj=0; j < n; ++j) { mask[i] |= mat[i][j] << j; } } intans=0, state = 1 << n; for (intset=0; set < state; ++set) { // the count of bit if not equal, then continue if (Integer.bitCount(set) != cols) { continue; } intcnt=0; for (int row : mask) { if ((row & set) == row) { ++cnt; } } ans = Math.max(ans, cnt); } return ans; } }
classSolution { List<List<Integer>> ans = newArrayList<List<Integer>>(); Stack<Integer> path = newStack<Integer>(); int k;
public List<List<Integer>> combine(int n, int k) { this.k = k; dfs(n); // enumerate from n to 1 have an effect that it is not necessary to preserve n return ans; }
voiddfs(int i) { // lopping -- to reduce the times of dfs intd= k - path.size(); // d represents the quantity of numbers that we can choose from if (d == 0) { // The search concludes if d = 0 ans.add(newArrayList<Integer>(path)); return ; } for (intj= i; j >= d; --j) { // lopping -- only numbers greater than d can be chosen // [d, i] -> i - d + 1 path.push(j); dfs(j - 1); path.pop(); } } }
classSolution { List<List<Integer>> ans = newArrayList<List<Integer>>(); Stack<Integer> path = newStack<Integer>(); int k;
public List<List<Integer>> combine(int n, int k) { this.k = k; dfs(n); // enumerate from n to 1 have an effect that it is not necessary to preserve n return ans; }
voiddfs(int i) { intd= k - path.size(); if (d == 0) { ans.add(newArrayList<Integer>(path)); return ; } if (i > d) { dfs(i - 1); } path.push(i); dfs(i - 1); path.pop(); } }
classSolution { List<List<Integer>> ans = newArrayList<List<Integer>>(); Stack<Integer> path = newStack<Integer>(); int k; public List<List<Integer>> combinationSum3(int k, int n) { this.k = k; dfs(9, n); return ans; } // range : [1, i], t : target voiddfs(int i, int t) { intd= k - path.size(); // the amount of numbers that can be chosen is d // lopping if (t < 0 || t > (i + i - d + 1) * d / 2) { // [a1, a2, ..., ad] the formula of sum return ; } if (d == 0) { ans.add(newArrayList<Integer>(path)); return ; } for (intj= i; j >= d; --j) { path.add(j); dfs(j - 1, t - j); path.pop(); } } }
classSolution { List<String> ans = newArrayList<String>(); char[] path; int m; public List<String> generateParenthesis(int n) { m = n << 1; path = newchar[m]; dfs(0, 0); return ans; } // i is the index of string, open is the amount of left brackets and the amount of right brackets is i - open voiddfs(int i, int open) { if (i == m) { ans.add(newString(path)); return ; } if (open < (m >> 1)) { // choose the left bracket path[i] = '('; dfs(i + 1, open + 1); } if (i - open < open) { // the amount of right brackets must less than left brackets path[i] = ')'; dfs(i + 1, open); } } }
classSolution { List<String> ans = newArrayList<String>(); Stack<Integer> path = newStack<Integer>(); int n; public List<String> generateParenthesis(int n) { this.n = n; dfs(0, 0); return ans; } // balance = the amount of left brackets - the amount of right brackets voiddfs(int i, int balance) { if (path.size() == n) { char[] s = newchar[n << 1]; Arrays.fill(s, ')'); for (int j : path) { s[j] = '('; } ans.add(newString(s)); return ; } // it can be filled right brackets from 0 to balance for (intclose=0; close <= balance; ++close) { path.add(i + close); // fill one left bracket dfs(i + close + 1, balance - close + 1); path.pop(); } } }
classSolution { List<List<String>> ans = newArrayList<List<String>>(); int[] column; // diag1 records the situation in the upper right that r + c = R + C // diag2 records the situation in the upper left that r - c = R - C boolean[] onPath, diag1, diag2; int n;
List<String> generateMapping() { List<String> ret = newArrayList<>(); char[] string = newchar[n]; Arrays.fill(string, '.'); for (int col : column) { string[col] = 'Q'; ret.add(newString(string)); string[col] = '.'; } return ret; }
voiddfs(int row) { if (row == n) { ans.add(generateMapping()); return ; } for (intcol=0; col < n; ++col) { // "row - col + n - 1" : n - 1 is to prevent the minus number if (!onPath[col] && !diag1[row + col] && !diag2[row - col + n - 1]) { column[row] = col; onPath[col] = diag1[row + col] = diag2[row - col + n - 1] = true; dfs(row + 1); onPath[col] = diag1[row + col] = diag2[row - col + n - 1] = false; } } }
public List<List<String>> solveNQueens(int n) { this.n = n; column = newint[n]; onPath = newboolean[n]; // both the max index of row and column are n - 1, so the range is [0, 2n - 2] and the maximum digit is (2n - 2) - (0) + 1 = 2n - 1 diag1 = newboolean[(n << 1) - 1]; diag2 = newboolean[(n << 1) - 1]; dfs(0); return ans; } }