[leetcode] 回溯法 Combination Sum 系列问题

原创 2016年08月30日 14:51:16

leetcde中回溯法:具体的方法,我认为手把手教你 < leetcode > 中的回溯算法——多一点套路这篇博文讲的很清晰。

下面是leetcode中Combination Sum 系列问题使用回溯法的问题

39 . Combination Sum
题目:
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]

题意:这个题是找出数组中相加=target的所有组合。

思路:求解该问题应该使用回溯法。

具体代码:

public class Solution {
    List<List<Integer>> result = new ArrayList<>();//保存最后结果

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        if(target < candidates[0] || candidates.length == 0 || candidates == null){
            return result;
        }
        List<Integer> list = new ArrayList<>();
        backTracking(candidates, target, 0, list);

        return result;
    }

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

    }
}

40 . Combination Sum II
题目:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

题意:找出所有数相加等于n的所有组合,注意每个数字只能用一次。
思路:使用回溯法。

代码如下:

public class Solution {
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        if(candidates[0] > target || candidates.length == 0 || candidates == null){
            return result;
        }
        List<Integer> list = new ArrayList<>();
        backTracking(candidates, target, 0, list);

        Set<List<Integer>> set = new HashSet<>(result);
        return new ArrayList<>(set);
    }

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

216 . Combination Sum III

问题:
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

题意:找出k个数相加等于n的所有组合。
思路:使用回溯法。

具体代码:

public class Solution {
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        if(n < k ||n >= k * 9){
            return result;
        }
        int[] candidates = new int[9];
        for(int i = 0; i < 9; i++){
            candidates[i] = i + 1;
        }
        List<Integer> list = new ArrayList<>();
        backTracking(candidates,k, n, 0, list);
        return result;
    }


    public void backTracking(int[] candidates, int k,int n, int start, List<Integer> list){
        if(n < 0){
            return;
        }else if( n == 0){
            if(list.size() == k)
                result.add(new ArrayList<>(list));
        }else{
            int len = candidates.length;

            for(int i = start; i < len; i++){
                list.add(candidates[i]);
                backTracking(candidates, k, n - candidates[i], i + 1, list);
                list.remove(list.size() - 1);
            }
        }
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode Shortest Palindrome (最短回文串)

Leetcode Shortest Palindrome (最短回文串) 题目描述 Given a string S, you are allowed to c...

Leetcode+牛客网—回文串总结(一)

关于常见面试题中回文串知识点的相关总结

【LeetCode004-005算法/编程练习C++】--两个数组的中间数,找出最长回文子串

LeetCode第四五题

Leetcode全回文问题

目录: 1、编号5 5_Longest Palindromic Substring 2、编号9 Palindrome Number 3、编号126 Valid Palindrome 4、编号132 P...

leetcode之回文系列

1 回文识别基础 回文通俗地将就是把一个句子或者字符串正过来反过来读都是一样的. 比如123321就是一个回文. 识别一个回文字符串思路也很简单:定义两个指针分别指向字符串的头尾,...

LeetCode 234: Palindrome Linked List (回文链表)

Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time a...

LeetCode:简化路径(Simplify Path)

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/...
  • ztf312
  • ztf312
  • 2016-04-04 15:47
  • 5085

[LeetCode] Palindrome Number & Valid Palindrome - 回文系列问题

题目概述: Determine whether an integer is a palindrome. Do this without extra space. 判断数字是否是回文 例如121、656...

leetcode Combination Sum II回溯问题

最近一直在看回溯算法的框架,这道题代表性比较强,用到剪枝(重复1),和约束(超过界限直接return)。 先贴下一般的回溯算法的框架。 引用自这个bolg http://blog.jqian.net/...

【LeetCode】 Combination Sum 系列

Combination Sum 【LeetCode】39.Combination Sum 【LeetCode】40.Combination Sum2
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)