# [置顶] [编程题] LeetCode上的backTracking类型的题目-难度Medium

494人阅读 评论(0)

backTracking链接：https://leetcode.com/tag/backtracking/

## 回溯的入门之子集-Subsets

public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backTracking(res, new ArrayList<>(), nums, 0);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int[] nums, int start){

res.add(new ArrayList<>(list)); //第一步 满足条件的临时集合加入结果集

for(int i = start; i < nums.length; ++i){ //第二步for循环 遍历所有的元素

list.add(nums[i]); //第三步 元素加入临时集合

backTracking(res, list, nums, i + 1); //第四步 回溯

list.remove(list.size() - 1); //第五步 元素从临时集合移除

}
}
}

## Subsets II

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
backTracking(res, new ArrayList<>(), nums, 0);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int[] nums, int start){
if(!res.contains(list)) res.add(new ArrayList<>(list)); //检查是否包含结果，不包含才加入
for(int i = start; i < nums.length; ++i){
list.add(nums[i]);
backTracking(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}

## Letter Combinations of a Phone Number

“23”

[“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]

public class Solution {
private String[] letter = new String[] {" ", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if(digits == null || digits.length() == 0) return res;
char[] nums = digits.toCharArray();
backTracking(res, new StringBuilder(), nums, 0);
return res;
}

private void backTracking(List<String> res, StringBuilder build, char[] nums, int start){
if(build.length() == nums.length) res.add(build.toString());
for(int i = start; i < nums.length; ++i){
int index = Integer.parseInt(String.valueOf(nums[i]));
for(int j = 0; j < letter[index].length(); ++j){
build.append(letter[index].charAt(j));
backTracking(res, build, nums, i + 1);
build.deleteCharAt(build.length() - 1);
}
}
}
}

## Generate Parentheses 括号生成器

public class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
backTracking(res, new StringBuilder(), 0, 0, n);
return res;
}

private void backTracking(List<String> res, StringBuilder builder, int left, int right, int n){
if(builder.length() == n*2) res.add(builder.toString());
else {
if(left < n){
builder.append("(");
backTracking(res, builder, left+1, right, n);
builder.deleteCharAt(builder.length()-1);
}
if(right < n && left > right){
builder.append(")");
backTracking(res, builder, left, right+1, n);
builder.deleteCharAt(builder.length()-1);
}

}
}
}

## Combinations

public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
backTracking(res, new ArrayList<>(), n, k, 1);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int n, int k, int start){
if(list.size() == k) res.add(new ArrayList(list));
for(int i = start; i <= n; ++i){
list.add(i);
backTracking(res, list, n, k, i + 1);
list.remove(list.size() - 1);
}
}
}

## Combination Sum

candidate ： [2, 3, 6, 7] | target： 7

public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
backTracking(res, new ArrayList<>(), candidates, target, 0);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int[] candidate, int target, int start){
if(target < 0) return;
if(target == 0) res.add(new ArrayList<>(list));
else {
for(int i = start; i < candidate.length; ++i){
list.add(candidate[i]);
backTracking(res, list, candidate, target - candidate[i], i);
list.remove(list.size() - 1);
}
}
}
}

## Combination Sum II

candidate ： [10, 1, 2, 7, 6, 1, 5]
target： 8

public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
backTracking(res, new ArrayList<>(), candidates, target, 0);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int[] candidate, int target, int start){
if(target < 0) return;
if(target == 0) res.add(new ArrayList<>(list));
else {
for(int i = start; i < candidate.length; ++i){
if(i > start && candidate[i] == candidate[i - 1]) continue;
list.add(candidate[i]);
backTracking(res, list, candidate, target - candidate[i], i + 1);
list.remove(list.size() - 1);
}
}
}
}

## Combination Sum III

public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
backTracking(res, new ArrayList<>(), k, n, 1);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int k, int n, int start){
if(n < 0) return;
if(n == 0 && list.size() == k) res.add(new ArrayList<>(list));
for(int i = start; i <= 9; i++){
list.add(i);
backTracking(res, list, k, n-i, i+1);
list.remove(list.size()-1);
}
}
}

## Permutations 排列

public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backTracking(res, new ArrayList<>(), nums);
return res;
}

private void backTracking(List<List<Integer>> res, List<Integer> list, int[] nums){
if(list.size() == nums.length) res.add(new ArrayList<>(list));
for(int i = 0; i < nums.length; ++i){
if(list.contains(nums[i])) continue; // 跳过重复的
list.add(nums[i]);
backTracking(res, list, nums);
list.remove(list.size() - 1);
}
}
}

## Palindrome Partitioning 分割回文字串

[
[“aa”,”b”],
[“a”,”a”,”b”]
]

public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
backTracking(res, new ArrayList<>(), s, 0);
return res;
}

private void backTracking(List<List<String>> res, List<String> list, String s, int start){
if(start == s.length()) res.add(new ArrayList<>(list)); //start == s.length()的时候，说明已经带s的最后一个字符了
else {
for(int i = start; i < s.length(); i++){
if(isPalindrome(s, start, i)){
list.add(s.substring(start, i+1));
backTracking(res, list, s, i+1);
list.remove(list.size() - 1);
}
}
}
}

/*
* 判断是否是回文
*/
private boolean isPalindrome(String s, int low, int high){
while(low < high){
if(s.charAt(low++) != s.charAt(high--)) return false;
}
return true;
}
}

## N-Queens

public class Solution {
public List<List<String>> solveNQueens(int n) {
List<Integer[]> res = new ArrayList<>();
backTracking(res, new Integer[n], n, 0);

//以下只是把皇后放置在棋盘上
List<List<String>> resList = new ArrayList<>();
for(Integer[] nums: res){
List<String> list = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
StringBuilder sb = new StringBuilder();
for(int j = 0; j < n; j++){
if(j == nums[i]) sb.append("Q");
else sb.append(".");
}
list.add(sb.toString());
}
resList.add(list);
}
return resList;
}

/*
* queen[] 是表示：下表i代表第i行的第queen[i]列放置一个皇后
*/
private void backTracking(List<Integer[]> res, Integer[] queen, int n, int start){
if(start == n) res.add(queen.clone());
else {
for(int i = 0; i < n; i++){ //不是从start开始
if(checkValid(queen, start, i)){
queen[start] = i;
backTracking(res, queen, n, start+1); //不是i+1，i只是和列有关
}
}
}

}

/*
* 检查在位置（row1，col1）是否可以放置皇后
*/
private boolean checkValid(Integer[] queen, int row1, int col1){
for(int row2 = 0; row2 < row1; row2++){
int col2 = queen[row2];
if(col2 == col1) return false; //两个皇后放置在同一列上
int colSize = Math.abs(col1 - col2);
int rowSize = row1 - row2;
if(colSize == rowSize) return false; //两个皇后放置在同一对角线上
}
return true;
}
}
2
0

个人资料
• 访问：16611次
• 积分：453
• 等级：
• 排名：千里之外
• 原创：26篇
• 转载：0篇
• 译文：0篇
• 评论：2条
文章分类
最新评论