LeetCode-Backtracking
Posted nomad1c
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Backtracking相关的知识,希望对你有一定的参考价值。
17. Letter Combinations of a Phone Number 电话号码的字母组合
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
题目:如果字符串包含来自2-9的数字,则返回数字可以表示的所有可能的字母组合。数字到字母的映射(就像在电话按钮上一样)如下所示。注意,1不映射到任何字母。
思路:
class Solution private static final String[] KEYS = "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" ; public List<String> letterCombinations(String digits) List<String> res = new LinkedList<String>(); if(digits.equals("")) return res; combination("", digits, 0, res); return res; private void combination(String prefix, String digits, int offset, List<String> res) if (offset >= digits.length()) res.add(prefix); return; String letters = KEYS[(digits.charAt(offset) - ‘0‘)]; for (int i = 0; i < letters.length(); i++) combination(prefix + letters.charAt(i), digits, offset + 1, res);
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;
以上是关于LeetCode-Backtracking的主要内容,如果未能解决你的问题,请参考以下文章