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);
                }
            }
        }
    }
    

      

  • 相关阅读:
    yii分页
    ajax分页
    批删,全选
    网站开发的愿景
    margin collapse 坍塌
    URI URL URN
    Servlet
    Http请求
    进程间通信
    网络编程
  • 原文地址:https://www.cnblogs.com/lautsie/p/3332324.html
Copyright © 2011-2022 走看看