Every time I encounter dfs, I won't do backtracking. After answering a few questions, I can probably summarize the template, and I will add more routines in the future.
public List<String> function(String s){
List<String> res = new ArrayList<String>();
dfs(res,strat,temp);
}
public void dfs(List<String> int start,StringBuilder temp){
//出口条件
if(***){
//出口时要做的事
res.add(temp.toString());
return ;
}
if(){
temp.append(); //添加一个
dfs(); //继续dfs
temp.deleteCharAt(); //回退到上一步
}
}
It is probably such a template, and the specific conditions are based on the requirements of the question.
Here are a few of the original questions in Lituo:
Question description Leetcode interview question 08.07. Permutations and combinations of non-repeating strings
Permutations and combinations of strings without duplication. Write a method that calculates all permutations of a string, where each character of the string is different.
Example 1:
Input: S = "qwe"
Output: ["qwe", "qew", "wqe", "weq", "ewq", "eqw"]
Example 2:Input: S = "ab"
Output: ["ab", "ba"]
Hint:The characters are all English letters.
The string length is between [1, 9].
Code:
class Solution {
List<String> list = new ArrayList<>();
boolean[] used = new boolean[10];
public String[] permutation(String S) {
StringBuilder sb = new StringBuilder();
helper(S,sb);
String[] res = new String[list.size()];
for(int i=0;i<list.size();i++){
res[i] = new String();
res[i] = list.get(i);
}
return res;
}
public void helper(String S,StringBuilder sb){
if(sb.length()==S.length()){
list.add(sb.toString());
return ;
}
for(int i=0;i<S.length();i++){
if(!used[i]){
sb.append(S.charAt(i));
used[i] = true;
helper(S,sb);
used[i]=false;
sb.deleteCharAt(sb.length()-1);
}
}
}
}
Leetcode interview question 08.04. Power set
Power set. Write a method that returns all subsets of a collection. The collection does not contain duplicate elements.
Explanation: The solution set cannot contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2 ],
[]
]
Code:
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
dfs(res,new LinkedList<Integer>(),nums,0);
return res;
}
public void dfs(List<List<Integer>> res,List<Integer> temp,int[] nums,int start){
res.add(new LinkedList<>(temp));
for(int i=start;i<nums.length;i++){
temp.add(nums[i]);
dfs(res,temp,nums,i+1);
temp.remove(temp.size()-1);
}
}
}
Leetcode Interview Question 08.09. Brackets
brackets. Design an algorithm that prints all legal (for example, one-to-one correspondence between open and closed) combinations of n pairs of brackets.
Explanation: The solution set cannot contain duplicate subsets.
For example, given n = 3, the generated result is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
Code:
class Solution {
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
StringBuilder sb = new StringBuilder();
//生成的每一个有效括号的长度都是2n
helper(0,n*2,sb);
return res;
}
public void helper(int leftCou,int n,StringBuilder sb){
//如果当前的括号已经是有效括号则加如结果集中
if(sb.length()==n){
res.add(sb.toString());
return;
}
//如果当前sb中的左括号个数大于0,则添加一个右括号,递归,左括号可加入个数-1
if(leftCou>0){
sb.append(')');
helper(leftCou-1,n,sb);
sb.deleteCharAt(sb.length()-1);
}
//如果当前还需要插入左右括号才能生成有效括号,则先插入左括号
if(n-sb.length()>leftCou){
sb.append('(');
helper(leftCou+1,n,sb);
sb.deleteCharAt(sb.length()-1);
}
}
}