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);
            }
        }
    }
    
    
  • 相关阅读:
    WinForm 下的 Wizard(向导) 控件, 提供设计时支持!
    关于安装VS2005或MSDN时遇到“Please insert the disk:XXXXXXX” 错误提示的解决方法!
    Welcome
    Latex 入门教程
    图形学 旋转与投影矩阵3
    图形学 旋转与投影矩阵—2
    算法希尔排序可视化
    图形学 旋转与投影矩阵—1
    BlinnPhong反射模型实践(web实现)
    贝塞尔曲线(面)二三维可视化(Three+d3)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4749415.html
Copyright © 2011-2022 走看看