zoukankan      html  css  js  c++  java
  • [leetcode]Combination Sum

    有了之前的经验,此题相对顺利。

    public class Solution {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
            ans.clear();
            tmp.clear();
            sub(candidates, 0, 0, target);
            return ans;
        }
        
        private void sub(int[] candidates, int pos, int sum, int target)
        {
            if (sum > target) return;
            else if (sum == target)
            {
                ArrayList<Integer> arr = new ArrayList<Integer>(tmp);
                Collections.sort(arr);
                ans.add(arr);
            }
            else
            {
                for (int i = pos; i < candidates.length; i++)
                {
                    tmp.add(candidates[i]);
                    sub(candidates, i, candidates[i]+sum, target);
                    tmp.remove(tmp.size()-1);
                }
            }
        }
    }
    

      

  • 相关阅读:
    @codeforces
    @codeforces
    @hdu
    @hdu
    @bzoj
    @bzoj
    @topcoder
    推荐系统主题相关资料
    Python统计百分比及排序
    如何发布及部署asp.net网站
  • 原文地址:https://www.cnblogs.com/lautsie/p/3332324.html
Copyright © 2011-2022 走看看