回溯面试常考题

Posted 赵jc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了回溯面试常考题相关的知识,希望对你有一定的参考价值。

二叉树和为某一路径值

https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/

class Solution {
    List<List<Integer>> ret = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        if(root == null) {
            return ret;
        }
        dfs(root, target);
        return ret;
    }
    public void dfs(TreeNode root, int target) {
        if(root == null) {
            return ;
        }
        path.add(root.val);
        target -= root.val;
        if(target == 0 && root.left == null && root.right == null) {
            ret.add(new LinkedList(path));
        }
        dfs(root.left, target);
        dfs(root.right, target);
        path.removeLast();
    }
}

没有重复数字的所有排列

https://leetcode-cn.com/problems/permutations/

class Solution {
    public List<List<Integer>> ret = new ArrayList<>();
    public List<Integer> path = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        if(nums == null || nums.length == 0) {
            return ret;
        }
        int n = nums.length;
        boolean[] used = new boolean[n];
        dfs(nums, n, 0, used, ret, path);
        return ret;
    }
    public void dfs(int[] nums, int len, int depth, boolean[] used, List<List<Integer>> ret,
    List<Integer> path) {
        if(depth == len) {
            ret.add(new ArrayList<>(path));
        }
        for(int i = 0; i < nums.length; i++) {
            if(!used[i]) {
                path.add(nums[i]);
                used[i] = true;
                dfs(nums, len, depth + 1, used, ret, path);
                used[i] = false;
                path.remove(path.size() - 1);
            }
        }
    } 
}

有重复数字的所有排列

import java.util.*;

public class Solution {
    ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
    ArrayList<Integer> path = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> permuteUnique(int[] nums) {
        if(nums == null || nums.length == 0) {
            return ret;
        }
        Arrays.sort(nums);
        int n = nums.length;
        boolean[] used = new boolean[n];
        dfs(nums, n, 0, used, ret, path);
        return ret;
    }
    public void dfs(int[] nums, int len, int depth, boolean[] used, ArrayList<ArrayList<Integer>> ret, ArrayList<Integer> path) {
        if(len == depth) {
            ret.add(new ArrayList<>(path));
        }
        for(int i = 0; i < nums.length; i++) {
           if(used[i]) {
               continue;
           }
            if(i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            dfs(nums, len, depth + 1, used, ret, path);
            used[i] = false;
            path.remove(path.size() - 1);
        }
    }
}

字符串的排列

class Solution {
    List<String> ret = new ArrayList<>();
    public String[] permutation(String s) {
        if(s.length() == 0) {
            return new String[0];
        }
        int n = s.length();
        char[] str = s.toCharArray();
        Arrays.sort(str);
        StringBuffer path = new StringBuffer();
        boolean[] used = new boolean[n];
        dfs(str, n, 0, used, ret, path);
        return ret.toArray(new String[0]);
    }
    public void dfs(char[] str, int len, int depth, boolean[] used, List<String> ret, StringBuffer path ) {
        if(depth == len) {
            ret.add(path.toString());
        }
        for(int i = 0; i < len; i++) {
         if(used[i]) {
             continue;
         }
        
        if(i > 0 && str[i] == str[i - 1] && !used[i - 1]) {
            continue;
        }
        path.append(str[i]);
        used[i] = true;
        dfs(str, len, depth + 1, used, ret, path);
        used[i] = false;
        path.deleteCharAt(path.length() - 1);
         }
    }
}

以上是关于回溯面试常考题的主要内容,如果未能解决你的问题,请参考以下文章

链表面试-链表分割(常考题)

python面试总结2(函数常考题和异常处理)

LeetCode面试常考题:Two Sum

LeetCode面试常考题:Two Sum

Python知识点——面试常考题

7-2 HTTP 面试常考题