Compare commits
2 Commits
610048f4ea
...
08ddcc8929
Author | SHA1 | Date | |
---|---|---|---|
|
08ddcc8929 | ||
|
dd90fb290f |
30
5.leetcode/src/Q17.java
Normal file
30
5.leetcode/src/Q17.java
Normal file
@ -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<String> res = new HashSet<>();
|
||||||
|
private StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
private String[] map = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
|
||||||
|
|
||||||
|
public List<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
5.leetcode/src/com/fanxb/common/Q22.java
Normal file
51
5.leetcode/src/com/fanxb/common/Q22.java
Normal file
@ -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<String> ans;
|
||||||
|
//左括号个数
|
||||||
|
int lCount;
|
||||||
|
//右括号个数
|
||||||
|
int rCount;
|
||||||
|
StringBuilder temp;
|
||||||
|
|
||||||
|
public List<String> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -33,6 +33,35 @@ public class Q39 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class NewSolution {
|
||||||
|
private List<List<Integer>> res;
|
||||||
|
private List<Integer> temp;
|
||||||
|
private int sum;
|
||||||
|
|
||||||
|
public List<List<Integer>> 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) {
|
public static void main(String[] args) {
|
||||||
new Q39().combinationSum(new int[]{2, 3, 5}, 8).forEach(System.out::println);
|
new Q39().combinationSum(new int[]{2, 3, 5}, 8).forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.fanxb.common;
|
package com.fanxb.common;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
@ -43,6 +44,37 @@ public class Q46 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class NewSolution {
|
||||||
|
private List<List<Integer>> res;
|
||||||
|
private List<Integer> temp;
|
||||||
|
private boolean[] cache;
|
||||||
|
|
||||||
|
public List<List<Integer>> 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) {
|
public static void main(String[] args) {
|
||||||
System.out.println(new Q46().permute(new int[]{1, 1, 2}));
|
System.out.println(new Q46().permute(new int[]{1, 1, 2}));
|
||||||
}
|
}
|
||||||
|
40
5.leetcode/src/com/fanxb/common/Q52.java
Normal file
40
5.leetcode/src/com/fanxb/common/Q52.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Q52 {
|
||||||
|
private List<Integer[]> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
5.leetcode/src/com/fanxb/common/Q77.java
Normal file
31
5.leetcode/src/com/fanxb/common/Q77.java
Normal file
@ -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<List<Integer>> combine(int n, int k) {
|
||||||
|
List<List<Integer>> ans = new LinkedList<>();
|
||||||
|
List<Integer> temp = new ArrayList<>(k);
|
||||||
|
dfs(ans, temp, n, k, 1);
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dfs(List<List<Integer>> ans, List<Integer> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
5.leetcode/src/com/fanxb/common/Q79.java
Normal file
29
5.leetcode/src/com/fanxb/common/Q79.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user