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);
            }
        }
    }
    
    
  • 相关阅读:
    《将博客搬至CSDN》
    java-FileUtils(复制文件夹、复制文件、字符串直接写入文件中)(新手)
    java-FileUtils(读取、判断、获取)-(新手)
    一.MySQL入门基础
    二.压缩指令的应用
    一.档案与目录管理
    四.mysql演示银行转账
    三.实例演示insert/update/delect更新数据库
    二.数据库游标对象cursor与实例
    一.数据库连接对象connection
  • 原文地址:https://www.cnblogs.com/airwindow/p/4749415.html
Copyright © 2011-2022 走看看