Advertisement
exmkg

Untitled

Aug 23rd, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. class Solution {
  2.     public static List<List<Integer>> combine(int n, int k) {
  3.         List<List<Integer>> combs = new ArrayList<List<Integer>>();
  4.         combine(combs, new ArrayList<Integer>(), 1, n, k);
  5.         return combs;
  6.     }
  7.  
  8.     public static void combine(List<List<Integer>> combs, List<Integer> comb, int start, int n, int k) {
  9.         if (k == 0) {
  10.             combs.add(new ArrayList<Integer>(comb));
  11.             return;
  12.         }
  13.         for (int i = start; i <= n; i++) {
  14.             comb.add(i);
  15.             combine(combs, comb, i + 1, n, k - 1);
  16.             comb.remove(comb.size() - 1);
  17.         }
  18.     }
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement