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);
            }
        }
    }
    
    
  • 相关阅读:
    中断向量表
    内核进程的堆栈 [转]
    int指令理解
    Linux进程创建和结束
    Linux 信号signal处理机制
    wait和waitpid详解
    linux 如何清理僵尸进程
    API:System V & POSI
    shell
    Code POJ
  • 原文地址:https://www.cnblogs.com/airwindow/p/4749415.html
Copyright © 2011-2022 走看看