zoukankan      html  css  js  c++  java
  • Combinations

    recursion

     1 public class Solution {
     2     public ArrayList<ArrayList<Integer>> combine(int n, int k) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
     6         if(n < 1||k < 1)
     7             return null;
     8         ArrayList<Integer> list = new ArrayList<Integer>();
     9         generate(result, list, 1, n, k);
    10         return result;
    11     }
    12     
    13     private void generate(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int depth, int n, int k){
    14         if(list.size() == k){
    15             ArrayList<Integer> tmp = new ArrayList<Integer>();
    16             tmp.addAll(list);
    17             result.add(tmp);
    18             return;
    19         }
    20         
    21         for(int i=depth; i <= n; i++)
    22         {
    23             list.add(i);
    24             generate(result, list, i+1, n, k);
    25             list.remove(list.size()-1);
    26         }
    27     }
    28 }
  • 相关阅读:
    DELPHI美化界面
    WebSevice相关
    Hotmail邮件接收
    DHTMLEdit
    Eclipse 基础
    POP3相关
    DELPHI中GIF的使用
    javaaop
    RAD Studio 2010 启动报错"displayNotification: 内存不够" 解决办法
    编程之道
  • 原文地址:https://www.cnblogs.com/jasonC/p/3421511.html
Copyright © 2011-2022 走看看