LeetCode算法练习top100:(7)递归回溯

package top100.递归回溯;

import java.util.*;

public class TOP {
    //46. 不含重复数字的全排列
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        LinkedList<Integer> path = new LinkedList<>();
        boolean[] visited = new boolean[nums.length]; //保证结果唯一,记忆使用过的元素
        dfs(nums, visited, path);
        return res;
    }
    private void dfs(int[] nums, boolean[] visited, LinkedList<Integer> path) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
        } else if (path.size() < nums.length) {
            for (int i = 0; i < nums.length; i++) {
                if (!visited[i]) {
                    visited[i] = true;
                    path.add(nums[i]);
                    dfs(nums, visited, path);
                    //每个数字都有可能放在第一个位置,所以要回溯
                    path.removeLast();
                    visited[i] = false;
                }
            }
        }
    }

    //78. 子集
    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> path = new ArrayList<>();
        backtrack(nums, 0, path);
        return lists;
    }
    private void backtrack(int[] nums, int start, List<Integer> path) {
        lists.add(new ArrayList<>(path)); //所以的路径都组为结果
        for (int i = start; i < nums.length; i++) {
            path.add(nums[i]);
            backtrack(nums, i + 1, path);
            //每个数字都有可能放在第一个位置,所以要回溯
            path.remove(path.size() - 1);
        }
    }


    //17. 电话号码的字母组合
    //回溯法来解决n个for循环的问题
    List<String> res = new ArrayList<>();
    private final String[] MAP = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        if ("".equals(digits)) return res;
        char[] path = new char[digits.length()]; //路径的长度固定,直接用数组记录路径
        dfs(digits, 0, path);
        return res;
    }
    //递归
    private void dfs(String digits, int index, char[] path) {
        if (index == digits.length()) {
            res.add(new String(path));
        } else {
            for (Character c : MAP[digits.charAt(index) - '0'].toCharArray()) {
                path[index] = c;
                dfs(digits, index + 1, path); //不需要回溯
            }
        }
    }


    //39. 组合总和
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);  //需要先排序,排序是剪枝的前提
        LinkedList<Integer> path = new LinkedList<>();
        dfs(candidates, target, 0, path);
        return res;
    }
    private void dfs(int[] candidates, int target, int begin, LinkedList<Integer> path) {
//        if (target < 0) return; //被优化
        if (target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }
        //利用索引和循环实现重复取数字
        for (int i = begin; i < candidates.length; i++) {
            if (target - candidates[i] < 0) { //剪支优化,可提升1ms
                break;
            }
            path.add(candidates[i]);
            //进入下一轮递归可以重复取数字
            dfs(candidates, target - candidates[i], i, path);
            //每个元素都可以放在第一个位置
            path.removeLast();
        }
    }


    //22. 括号生成
    List<String> res = new ArrayList<>();
    public List<String> generateParenthesis(int n) {
        StringBuilder path = new StringBuilder();
        dfs(n, 0, 0, path); //左右括号数量为n
        return res;
    }
    private void dfs(int n, int left, int right, StringBuilder path) {
        if (left == n && right == n) {
            res.add(new String(path));
            return;
        }
        if (left < n) {
            path.append('(');
            dfs(n, left + 1, right, path);
            path.deleteCharAt(path.length() - 1);
        }
        if (right < left && right < n) {
            path.append(')');
            dfs(n, left, right + 1, path);
            path.deleteCharAt(path.length() - 1);
        }
    }


    //79. 单词搜索  搜索图
    int[][] ways = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
    boolean[][] visited;
    int m, n;
    public boolean exist(char[][] board, String word) {
        m = board.length;
        n = board[0].length;
        visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                visited[i][j] = true;
                if (board[i][j] == word.charAt(0) && dfs(board, word, 1, i, j)) {
                    return true;
                }
                visited[i][j] = false;
            }
        }
        return false;
    }
    private boolean dfs(char[][] board, String word, int index, int i, int j) {
        if (index == word.length()) {
            return true;
        }
        for (int[] way : ways) {
            int x = i + way[0], y = j + way[1];
            if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && board[x][y] == word.charAt(index)) {
                visited[x][y] = true;
                if (dfs(board, word, index + 1, x, y)) return true;
                visited[x][y] = false;
            }
        }
        return false;
    }


    //131. 分割回文串
    List<List<String>> lists = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();
    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return lists;
    }
    //把所有可能的分割方式都枚举出来
    private void backTracking(String s, int startIndex) {
        //如果起始位置大于s的大小,说明找到了一组分割方案
        if (startIndex >= s.length()) {
            lists.add(new ArrayList(deque));
            return;
        }
        //for:横向遍历,截取第一个字符串
        for (int i = startIndex; i < s.length(); i++) {
            //如果是回文子串,则记录
            if (isPalindrome(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                deque.addLast(str);
                //起始位置后移,保证不重复
                //递归:纵向遍历,对剩下的字符串继续分割
                backTracking(s, i + 1);
                //每个元素都可以作为起点
                deque.removeLast();
            }
        }
    }
    //判断是否是回文串
    private boolean isPalindrome(String s, int startIndex, int end) {
        for (int i = startIndex, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

你可能感兴趣的:(算法,leetcode,深度优先)