回溯算法

17. 电话号码的字母组合

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> list = new ArrayList<>();
        if (digits.length() == 0) {
            return list;
        }
        Map<Character, String> phonemap = new HashMap<>();
        phonemap.put('2',"abc");
        phonemap.put('3', "def");
        phonemap.put('4', "ghi");
        phonemap.put('5', "jkl");
        phonemap.put('6', "mno");
        phonemap.put('7', "pqrs");
        phonemap.put('8', "tuv");
        phonemap.put('9', "wxyz");
        backtrack(list, digits, phonemap, 0, new StringBuffer());
        return list;
    }

    public void backtrack(List<String> list, String digits, Map<Character, String> map, int index, StringBuffer sb) {
        if (index == digits.length()) {
            list.add(sb.toString());
            return;
        }
        char digit = digits.charAt(index);
        String str = map.get(digit);
        int length = str.length();
        for (int i = 0; i < length; i++) {
            sb.append(str.charAt(i));
            backtrack(list, digits, map, index+1, sb);
            sb.deleteCharAt(index);
        }
                
    }
}

22. 括号生成

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> list = new ArrayList();
        if(n==0) {
            return list;
        }
        backtrack(list, new StringBuffer(), 0, 0, n);
        return list;

    }

    public void backtrack(List<String> list, StringBuffer sb, int left, int right, int n) {
        if (sb.length() == n*2) {
            list.add(sb.toString());
            return;
        }

        if (left < n) {
            sb.append('(');
            backtrack(list, sb, left+1, right, n);
            sb.deleteCharAt(sb.length()-1);
        }

        if (right < left) {
            sb.append(')');
            backtrack(list, sb, left, right+1, n);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}

39. 组合总和

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> lists = new ArrayList<>();
        backtrack(lists, new ArrayList<Integer>(), candidates, target,0);
        return lists;
    }

    public void backtrack(List<List<Integer>> lists, List<Integer> list, int[] candidates, int target, int index) {

        if (target == 0) {
            lists.add(new ArrayList<Integer>(list));
            return;
        }
        if (target > 0) {
        //当i=index时,返回数组元素可重复,当i=index+1时,返回数组元素不可重复
            for (int i = index; i < candidates.length; i++) {
                list.add(candidates[i]);
                backtrack(lists,list,candidates,target-candidates[i], i);
                list.remove(list.size()-1);
            }
        } else {
            return;
        }
    }
}

40. 组合总和 II

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> lists = new ArrayList<>();
        if (candidates==null || candidates.length==0) {
            return lists;
        }
        Arrays.sort(candidates);
        helper(lists, new ArrayList<Integer>(), candidates, target, 0);
        return lists;
     }
     
    public void helper(List<List<Integer>> lists, List<Integer> list, int[] candidates, int target, int index) {
        if (target == 0) {
            lists.add(new ArrayList<Integer>(list));
            return;
        } else if (target > 0) {
            for (int i = index; i < candidates.length; i++) {
                // 减枝,去重
                if (i>index && candidates[i-1]==candidates[i]) continue;
                
                list.add(candidates[i]);
                helper(lists, list, candidates, target-candidates[i], i+1);     // index=i+1--每次只用一个
                list.remove(list.size()-1);
            } 
        } else return;
    }
}

46. 全排列

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        //visited数组用于判别某一值是否已排列
        boolean[] visited = new boolean[nums.length];	
        if (nums.length == 0 || nums == null) {
            return lists;
        }
        backtrack(lists, new ArrayList<Integer>(), nums, visited);
        return lists;
    }

    public void backtrack(List<List<Integer>> lists, List<Integer> list, int[] nums, boolean[] visited) {
        if (list.size() == nums.length) {
            lists.add(new ArrayList(list));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (visited[i]) continue;

            visited[i] = true;
            list.add(nums[i]);
            backtrack(lists, list, nums, visited);
            visited[i] = false;
            list.remove(list.size() - 1);
        }
    }
}

47. 全排列 II

class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        //visited数组用于判别某一值是否已排列
        boolean[] visited = new boolean[nums.length];
        if (nums.length == 0 || nums == null) {
            return lists;
        }
        Arrays.sort(nums);
        backtrack(lists, new ArrayList<Integer>(), nums, visited);
        return lists;
    }

    public void backtrack(List<List<Integer>> lists, List<Integer> list, int[] nums, boolean[] visited) {
        if (list.size() == nums.length) {
            lists.add(new ArrayList(list));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (visited[i]) continue;	//
            if (i>0 && nums[i]==nums[i-1] && visited[i-1]==true) continue;  //减枝,去重

            visited[i] = true;
            list.add(nums[i]);
            backtrack(lists, list, nums, visited);
            visited[i] = false;
            list.remove(list.size() - 1);
        }
    }
}

51. N 皇后

class Solution {
    public List<List<String>> solveNQueens(int n) {
        List<List<String>> lists = new ArrayList<>();
        boolean[][] board = new boolean[n][n];
        if (n == 0) {
            return lists;
        }
        Set<Integer> col = new HashSet<>();
        Set<Integer> pie = new HashSet<>();
        Set<Integer> na = new HashSet<>();
        helper(lists, n, board,0, col, pie, na);
        return lists;
    }

    public void helper(List<List<String>> lists, int n, boolean[][] board, int row, Set<Integer> col, Set<Integer> pie, Set<Integer> na) {
        if (row == n) {
            List<String> list = result(lists, board, n);
            lists.add(new ArrayList<String>(list));
            return;
        } 
        for (int column = 0; column < n; column++) {
            if (col.contains(column) || pie.contains(row + column) || na.contains(row - column)) continue;

            board[row][column] = true;     //将该位置放置Queue
            col.add(column);
            pie.add(row + column);
            na.add(row - column);

            helper(lists, n, board, row + 1, col, pie, na);

            board[row][column] = false;
            col.remove(column);
            pie.remove(row + column);
            na.remove(row - column);
        }
        
    }

    public List<String> result(List<List<String>> lists, boolean[][] board, int n) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < n; j++) {
                if (board[i][j]) {
                    sb.append("Q");
                }else
                    sb.append(".");
            }
            list.add(sb.toString());
        }
        return list;
    }
}

52. N皇后 II

class Solution {
    public int totalNQueens(int n) {
        List<List<Integer>> lists = new ArrayList<>();
        if (n == 0) {
            return 0;
        }
        boolean[][] board = new boolean[n][n];
        Set<Integer> column = new HashSet<>();
        Set<Integer> pie = new HashSet<>();
        Set<Integer> na = new HashSet<>();
        return helper(n, board, 0, column, pie, na);
    }

    public int helper(int n, boolean[][] board, int row, Set<Integer> column, Set<Integer> pie, Set<Integer> na) {
        int count = 0;
        if (row == n) {
            return 1;
        }
        for (int col = 0; col < n; col++) {
            if (column.contains(col) || pie.contains(row+col) || na.contains(row-col)) continue;

            board[row][col] = true;
            column.add(col);
            pie.add(row+col);
            na.add(row-col);

            count += helper(n, board, row+1, column, pie, na);

            board[row][col] = false;
            column.remove(col);
            pie.remove(row+col);
            na.remove(row-col);
        }
        return count;
    }
}

78. 子集

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        boolean[] visited = new boolean[nums.length];
        helper(lists, new ArrayList<Integer>(), nums, 0);
        return lists;
    }

    public void helper(List<List<Integer>> lists, List<Integer> list, int[] nums, int index) {
        lists.add(new ArrayList<Integer>(list));
        
        for (int i = index; i < nums.length; i++) {     
            list.add(nums[i]);
            //注意为i+1;
            helper(lists, list, nums, i+1);
            list.remove(list.size()-1);
        }
    }
}

90. 子集 II

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        boolean[] visited = new boolean[nums.length];
        Arrays.sort(nums);
        helper(lists, new ArrayList<Integer>(), nums, 0);
        return lists;
    }

    public void helper(List<List<Integer>> lists, List<Integer> list, int[] nums, int index) {
        if (!lists.contains(new ArrayList<>(list))) {	//去除相同子集
            lists.add(new ArrayList<Integer>(list));
        }

        for (int i = index; i < nums.length; i++) {
            list.add(nums[i]);
            //注意为i+1;
            helper(lists, list, nums, i+1);
            list.remove(list.size()-1);
        }
    }
}

131. 分割回文串

class Solution {
    public List<List<String>> partition(String s) {
        List<List<String>> lists = new ArrayList<>();
        if (s.length() == 0) return lists;
        helper(lists, new ArrayList<>(), s, 0);
        return lists;
    }

    //回溯
    private void helper(List<List<String>> lists, List<String> curList, String s, int lo) {
        if (lo == s.length()) {
            lists.add(new ArrayList(curList));
            return;
        }
        
        int n = s.length();
        for (int hi = lo; hi < n; hi++) {
            if (isPal(s, lo, hi)) {
                curList.add(s.substring(lo, hi+1));
                helper(lists, curList, s, hi + 1);
                curList.remove(curList.size() - 1);
            }
        }
    }

    //判断回文
    private boolean isPal(String s, int lo, int hi) {
        while(lo <= hi) {
            if (s.charAt(lo) != s.charAt(hi)) return false;
            lo++;
            hi--;
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值