zoukankan      html  css  js  c++  java
  • [LeetCode#216]Combination Sum III

    Problem:

    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.

    Ensure that numbers within the set are sorted in ascending order.


    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]]

    Analysis:

    This problem is similar with other combination problems. Actually it is more easy, since the element comes from the range[1, 9]
    The problem requires the solution must contain certain amount of numbers. 
    
    The same as the past routine, we use DFS to search for the right answer. For the wrong direction, we should be able to backtrack to its previous state.
    
    Base case:
    1. find out right answer.
    The target is just 0, which means its previos state has found out the answer(the state information recored in path).
    What's more, the path.size() must exactly equal to k. no k - 1 or k+1
    
    if (n == 0 && path.size() == k) {
        ret.add(new ArrayList<Integer> (path));
        return;
    }
    
    2. violation cases.
    2.a the last state exceeds the target
    2.b the start index exceeds the maximum index 9
    2.c the path.size exceeds k.
    
    if (n < 0 || start > 9 || path.size() >= k)
        return;
    
    Actually path.size() >= k could be writeen into path.size() == k
    Since the curent path has already included k elemennts, but it does not meet the previous checking:
    if (n == 0 && path.size() == k) {
    ...
    }
    We could stop the search along this direction when path.size() == k. 

    Solution:

    public class Solution {
        public List<List<Integer>> combinationSum3(int k, int n) {
            List<List<Integer>> ret = new ArrayList<List<Integer>> ();
            if (k <= 0 || n <= 0)
                return ret;
            ArrayList<Integer> path = new ArrayList<Integer> ();
            helper(1, k, n, path, ret);
            return ret;
        }
        
        private void helper(int start, int k, int n, ArrayList<Integer> path, List<List<Integer>> ret) {
            if (n == 0 && path.size() == k) {
                ret.add(new ArrayList<Integer> (path));
                return;
            }
            //chop off branches is very important
            if (n < 0 || start > 9 || path.size() == k)
                return;
            for (int i = start; i <= 9; i++) {
                path.add(i);
                helper(i+1, k, n-i, path, ret);
                path.remove(path.size()-1);
            }
        }
    }
    
    
  • 相关阅读:
    Abp Hangfire 占用 PostgreSql 连接数的另类解决方案
    Git 仓库中文件大小写重命名实践(Windows 环境)
    C# WinForm 文件夹选择控件 folderBrowserDialog 的应用实例
    使用 C# 修改文件创建时间(图片也可修改)
    ABP 在 EntityFramework 中使用扩展批量更新时的异常
    Visual Studio之“生成事件”实践
    博客专家纪念
    深入浅出-应用服务
    如何管理 .NET Core 工具
    深入浅出-可定制仓储设计
  • 原文地址:https://www.cnblogs.com/airwindow/p/4749415.html
Copyright © 2011-2022 走看看