1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.demo.s77;
import java.util.ArrayList; import java.util.List;
public class Solution { List<Integer> temp = new ArrayList<Integer>(); List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> combine(int n, int k) { dfs(1, n, k); return ans; }
public void dfs(int cur, int n, int k) { if (temp.size() + (n - cur + 1) < k) { return; } if (temp.size() == k) { ans.add(new ArrayList<Integer>(temp)); return; } temp.add(cur); dfs(cur + 1, n, k); temp.remove(temp.size() - 1); dfs(cur + 1, n, k); }
}
|