public class GenerateParenthesis { public staticList<String> generateParenthesis(int n) { List<String> result = new ArrayList<String>(); generateParenthesis(n, n , n, "", result); returnresult; } private staticvoid generateParenthesis(int left, int right, int n, String s, List<String> result) {
if (s.length() == n * 2) { result.add(s); } else { if (left == right) { generateParenthesis(left - 1, right, n , s + "(", result); } elseif (left < right) { if (left > 0) { generateParenthesis(left - 1, right, n , s + "(", result); } generateParenthesis(left, right - 1, n, s + ")", result); } } } public staticvoid main(String[] args) { List<String> result = generateParenthesis(3); for (String s: result) { System.out.println(s); } } }
public class GenerateParenthesis { public staticList<String> generateParenthesis(int n) { List<String> result = new ArrayList<String>(); char[] str = new char[n * 2]; generateParenthesis(n, n , str, 0, result); returnresult; } private staticvoid generateParenthesis(int left, int right, char[] str, int length, List<String> result) {
public class Permutation { public staticList<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if (nums.length == 0) returnresult; while (true) { List<Integer> temp = new ArrayList<Integer>(); for (int t: nums) { temp.add(t); } result.add(temp); int i = nums.length - 2; while (i >= 0 && nums[i] > nums[i + 1]) i--; if (i < 0) break;
int j = nums.length - 1; while (j > i && nums[i] > nums[j]) j--; swap(nums, i, j); reverse(nums, i + 1, nums.length - 1); } reverse(nums, 0, nums.length - 1); returnresult; } public staticvoid swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } public staticvoid reverse(int[] nums, int begin, intend) { while (begin < end) { swap(nums, begin, end); begin++; end--; } }
public staticvoid main(String[] args) { int[] nums = {1, 2, 3, 4}; List<List<Integer>> result = permute(nums); for (List<Integer> list: result) { for (Integer i: list) { System.out.print(i); } System.out.println(""); } for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " "); } } }
publicclass NQueens { publicstaticinttotalNQueens(int n){ boolean[][] board = new boolean[n][n]; return totalNQueens(0, n, board); } //check if queen can put on board[row][col] privatestatic boolean canPutCheck(int row, int col, int n, boolean[][] board){ for (int i = 0; i < n; i++) { if (board[row][i]) //row returnfalse; if (board[i][col]) //col returnfalse; } //diagonal int i = 0; while (row + i < n && col + i < n) { if (board[row + i][col +i]) returnfalse; i++; } i = 0; while (row - i >= 0 && col - i >= 0) { if (board[row - i][col - i]) returnfalse; i++; } //back diagonal i = 0; while (row + i < n && col - i >= 0) { if (board[row + i][col - i]) returnfalse; i++; } i = 0; while (row - i >= 0 && col + i < n) { if (board[row - i][col + i]) returnfalse; i++; } returntrue;
} privatestaticinttotalNQueens(int row, int n, boolean[][] board){ if (row == n) { return1; } int count = 0; for (int j = 0; j < n; j++) { if (canPutCheck(row, j, n, board)) { board[row][j] = true; count += totalNQueens(row + 1, n, board); board[row][j] = false; //backtrack } } return count;
} publicstaticvoidmain(String[] args){ for (int i = 4; i < 10; i++) System.out.println(i + " " + totalNQueens(i)); } }