LeetCode-Backtracking

37. Sudoku Solver 解答数独

https://leetcode.com/problems/sudoku-solver/

题目:编写一个程序,通过填充空单元格来解决数独难题。

思路:

39. Combination Sum 数字组合之和

https://leetcode.com/problems/combination-sum/

题目:给定一组候选数字(无重复项)和一个目标数字(目标),在候选数字中寻找和为目标数字的候选数字的唯一组合。数字可以重复选择。

思路:

class Solution {
    private List<List<Integer>> res;

    private List<Integer> cur;

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        res = new ArrayList<List<Integer>>();
        cur = new ArrayList<Integer>();
        helper(candidates, target, 0); 
        return res;
    }

    private void helper(int[] candidates, int target, int i) {
        for(int n : cur) {
            System.out.print(n + " ");
        }
        System.out.println();
        if (target == 0) {
            res.add(new ArrayList<>(cur));
        } else if (target < 0) {
            return;
        } else {
            while (i < candidates.length) {
                // i -> lastIndex
                int candidate = candidates[i];
                cur.add(candidate);
                helper(candidates, target - candidate, i);
                cur.remove(cur.size() - 1);
                i++;
            }
        }
    }
}

46. Permutations 排列

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

题目:给定一个不同整数集合,返回所有可能的排列。

思路:

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> init = new ArrayList<>();
        for(int i = 0; i < nums.length; i++) {
            init.add(nums[i]);
        }
        permutation(init, new ArrayList<>(), res);
        return res;
    }
    
    private void permutation(List<Integer> init, List<Integer> list, List<List<Integer>> res) {
        if(init.isEmpty()) {
            res.add(new ArrayList<>(list));
        } else {
            for (int i = 0; i < init.size(); i++) {
                List<Integer> initD = new ArrayList<>(init);
                List<Integer> listD = new ArrayList<>(list);
                listD.add(initD.get(i));
                initD.remove(i);
                permutation(initD, listD, res);
            }
        }
    }
}

47. Permutations II 排列 II

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

题目:给定可能包含重复项的数字集合,则返回所有可能的唯一排列。

思路:

class Solution {   
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(nums==null || nums.length==0) return res;
        boolean[] used = new boolean[nums.length];
        List<Integer> list = new ArrayList<Integer>();
        Arrays.sort(nums);
        dfs(nums, used, list, res);
        return res;
    }

    public void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> res){
        if(list.size()==nums.length){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i=0;i<nums.length;i++){
            if(used[i]) continue;
            if(i>0 &&nums[i-1]==nums[i] && !used[i-1]) continue;
            used[i]=true;
            list.add(nums[i]);
            dfs(nums,used,list,res);
            used[i]=false;
            list.remove(list.size()-1);
        }
    }
}

51. N-Queens N皇后问题

https://leetcode.com/problems/n-queens/

题目:在n×n棋盘上放置n个皇后,使得不会有两个皇后能够互相攻击的情况出现。

思路:

class Solution {
    public List<List<String>> solveNQueens(int n) {
        List<List<String>> result = new ArrayList<>();
        char[][] board = new char[n][n];
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                board[i][j] = '.';
            }
        }
        solveNQueens(n, 0, new int[n], board, result);
        return result;
    }
    
    public void solveNQueens(int n, int row, int[] occupied, char[][] board, List<List<String>> result) {
        // our goal
        if(row == n) {
            List<String> path = new ArrayList<>();
            for (int i=0; i<n; i++) {
                path.add(String.valueOf(board[i]));
            }
            result.add(path);
            // return;
        } else {
            for(int col = 0; col < n; col++) {
                // our choice
                occupied[row] = col;
                board[row][col] = 'Q';
                // our constraints
                if(isValid(occupied, row, col)) {
                    solveNQueens(n, row+1, occupied, board, result);
                }
                // undo our choice
                board[row][col] = '.';
            }
        }
    }
    
    public boolean isValid(int[] occupied, int row, int col) {
        for(int i = 0; i < row; i++) {
            if (occupied[i] == col || Math.abs(col - occupied[i]) == Math.abs(i - row)) {
                return false;
            }
        }
        return true;
    }
}

猜你喜欢

转载自www.cnblogs.com/nomad1c/p/11614000.html