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);
            }
        }
    }
    
    
  • 相关阅读:
    springBoot(3)---目录结构,文件上传
    springBoot(2)---快速创建项目,初解jackson
    VueJs(14)---理解Vuex
    VueJs(13)---过滤器
    VueJs(12)---vue-router(导航守卫,路由元信息,获取数据)
    php多进程中的阻塞与非阻塞
    php 中的信号处理
    dede中arcurl的解析
    dede5.7 GBK 在php5.4环境下 后台编辑器无法显示文章内容
    php5.3 php-fpm 开启 关闭 重启
  • 原文地址:https://www.cnblogs.com/airwindow/p/4749415.html
Copyright © 2011-2022 走看看