diff --git a/5.leetcode/src/Q17.java b/5.leetcode/src/Q17.java new file mode 100644 index 0000000..83c2986 --- /dev/null +++ b/5.leetcode/src/Q17.java @@ -0,0 +1,30 @@ +package com.fanxb.common; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Q17 { + private Set res = new HashSet<>(); + private StringBuilder stringBuilder = new StringBuilder(); + private String[] map = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + + public List letterCombinations(String digits) { + if (digits.isEmpty()) return new ArrayList<>(); + dfs(digits.length(), digits, 0); + return new ArrayList<>(res); + } + + private void dfs(int size, String digits, int i) { + if (stringBuilder.length() == size) { + res.add(stringBuilder.toString()); + return; + } + for (char c : map[digits.charAt(i) - '0'].toCharArray()) { + stringBuilder.append(c); + dfs(size, digits, i + 1); + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q22.java b/5.leetcode/src/com/fanxb/common/Q22.java new file mode 100644 index 0000000..2e426bc --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q22.java @@ -0,0 +1,51 @@ +package com.fanxb.common; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Q22 { + Set ans; + //左括号个数 + int lCount; + //右括号个数 + int rCount; + StringBuilder temp; + + public List generateParenthesis(int n) { + ans = new HashSet<>(); + lCount = 0; + rCount = 0; + temp = new StringBuilder(); + dfs(n, 2 * n); + return new ArrayList<>(ans); + } + + private void dfs(int n, int total) { + if (temp.length() == total) { + ans.add(temp.toString()); + return; + } + if (lCount < n) { + lCount++; + temp.append('('); + dfs(n, total); + lCount--; + temp.deleteCharAt(temp.length() - 1); + } + if (rCount < lCount) { + rCount++; + temp.append(')'); + dfs(n, total); + rCount--; + temp.deleteCharAt(temp.length() - 1); + } + } + + public static void main(String[] args) { + long start = System.currentTimeMillis(); + System.out.println(new Q22().generateParenthesis(8)); + System.out.println(System.currentTimeMillis() - start); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q39.java b/5.leetcode/src/com/fanxb/common/Q39.java index c869967..7abeded 100644 --- a/5.leetcode/src/com/fanxb/common/Q39.java +++ b/5.leetcode/src/com/fanxb/common/Q39.java @@ -33,6 +33,35 @@ public class Q39 { } } + private class NewSolution { + private List> res; + private List temp; + private int sum; + + public List> combinationSum(int[] candidates, int target) { + res = new ArrayList<>(); + temp = new ArrayList<>(); + sum = 0; + dfs(candidates, target, 0); + return res; + } + + private void dfs(int[] candidates, int target, int cur) { + if (sum > target) return; + if (sum == target) { + res.add(new ArrayList<>(temp)); + return; + } + for (int i = cur; i < candidates.length; i++) { + temp.add(candidates[i]); + sum += candidates[i]; + dfs(candidates, target, i); + sum -= candidates[i]; + temp.remove(temp.size() - 1); + } + } + } + public static void main(String[] args) { new Q39().combinationSum(new int[]{2, 3, 5}, 8).forEach(System.out::println); } diff --git a/5.leetcode/src/com/fanxb/common/Q46.java b/5.leetcode/src/com/fanxb/common/Q46.java index 9ed38be..afd4432 100644 --- a/5.leetcode/src/com/fanxb/common/Q46.java +++ b/5.leetcode/src/com/fanxb/common/Q46.java @@ -1,6 +1,7 @@ package com.fanxb.common; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.Stack; @@ -43,6 +44,37 @@ public class Q46 { } } + private static class NewSolution { + private List> res; + private List temp; + private boolean[] cache; + + public List> permute(int[] nums) { + res = new LinkedList<>(); + temp = new ArrayList<>(nums.length); + cache = new boolean[nums.length]; + dfs(nums, nums.length); + return res; + } + + private void dfs(int[] nums, int length) { + if (temp.size() == nums.length) { + res.add(new ArrayList<>(temp)); + return; + } + for (int i = 0; i < length; i++) { + if (!cache[i]) { + temp.add(nums[i]); + cache[i] = true; + dfs(nums, length); + cache[i] = false; + temp.remove(temp.size() - 1); + } + } + } + } + + public static void main(String[] args) { System.out.println(new Q46().permute(new int[]{1, 1, 2})); } diff --git a/5.leetcode/src/com/fanxb/common/Q52.java b/5.leetcode/src/com/fanxb/common/Q52.java new file mode 100644 index 0000000..c395d43 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q52.java @@ -0,0 +1,40 @@ +package com.fanxb.common; + +import java.util.ArrayList; +import java.util.List; + +public class Q52 { + private List state; + //记录列上的皇后 + private boolean[] col; + private int res; + + + public int totalNQueens(int n) { + state = new ArrayList<>(n); + col = new boolean[n]; + res = 0; + dfs(n, 0); + return res; + } + + private void dfs(int n, int cur) { + if (state.size() == n) { + res++; + return; + } + for (int i = 0; i < n; i++) { + //当前行或者当前列已经放了,那么跳过 + if (col[i]) continue; + //判断斜线上有没有存在的皇后,判断方法:主对角线行减列的差值相等,次对角线的行加列值相等 + int val1 = cur - i, val2 = cur + i; + if (state.stream().anyMatch(item -> item[0] - item[1] == val1 || item[0] + item[1] == val2)) continue; + state.add(new Integer[]{cur, i}); + col[i] = true; + //往下一行放 + dfs(n, cur + 1); + state.remove(state.size() - 1); + col[i] = false; + } + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q77.java b/5.leetcode/src/com/fanxb/common/Q77.java new file mode 100644 index 0000000..376d879 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q77.java @@ -0,0 +1,31 @@ +package com.fanxb.common; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class Q77 { + public List> combine(int n, int k) { + List> ans = new LinkedList<>(); + List temp = new ArrayList<>(k); + dfs(ans, temp, n, k, 1); + return ans; + } + + private void dfs(List> ans, List temp, int n, int k, int cur) { + if (temp.size() == k) { + ans.add(new ArrayList<>(temp)); + return; + } + for (int i = cur; i <= n; i++) { + temp.add(i); + dfs(ans, temp, n, k, i + 1); + temp.remove(temp.size() - 1); + } + } + + public static void main(String[] args) { + new Q77().combine(4, 2); + } + +} diff --git a/5.leetcode/src/com/fanxb/common/Q79.java b/5.leetcode/src/com/fanxb/common/Q79.java new file mode 100644 index 0000000..dba5a6d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q79.java @@ -0,0 +1,30 @@ +package com.fanxb.common; + +public class Q79 { + private int[][] step = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + + public boolean exist(char[][] board, String word) { + char[] chars = word.toCharArray(); + int m = board.length, n = board[0].length; + cache = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (dfs(board, chars, 0, i, j, m, n)) return true; + } + } + return false; + } + + private boolean dfs(char[][] board, char[] word, int k, int x, int y, int m, int n) { + if (word[k] != board[x][y]) return false; + if (k == word.length - 1) return true; + board[x][y] = ' '; + for (int[] item : step) { + int x1 = x + item[0], y1 = y + item[1]; + if (x1 < 0 || x1 >= m || y1 < 0 || y1 >= n || board[x1][y1] == ' ') continue; + if (dfs(board, word, k + 1, x1, y1, m, n)) return true; + } + board[x][y] = word[k]; + return false; + } +}