package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

public class _77 {

    public static class Solution1 {
        public List<List<Integer>> combine(int n, int k) {
            List<List<Integer>> result = new ArrayList();
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = i + 1;
            }
            backtracking(k, 0, nums, new ArrayList(), result);
            return result;
        }

        void backtracking(int k, int start, int[] nums, List<Integer> curr, List<List<Integer>> result) {
            if (curr.size() == k) {
                result.add(new ArrayList(curr));
            } else if (curr.size() < k) {
                for (int i = start; i < nums.length; i++) {
                    curr.add(nums[i]);
                    backtracking(k, i + 1, nums, curr, result);
                    curr.remove(curr.size() - 1);
                }
            }
        }
    }
}

@@ -137,16 +137,157 @@
 tion1 %7B%0A
+        /**%0A         * I'm glad that I worked this one out completely on my own on 10/11/2021! Enjoy the beauty of backtracking!%0A         */%0A
         
@@ -369,22 +369,19 @@
 teger%3E%3E 
-result
+ans
  = new A
@@ -388,16 +388,18 @@
 rrayList
+%3C%3E
 ();%0A    
@@ -410,122 +410,146 @@
     
+for (
 int
-%5B%5D
  num
-s
  = 
-new int%5Bn%5D;%0A            for (int i = 0; i %3C n; i++) %7B%0A                nums%5Bi%5D = i + 1;%0A            %7D%0A
+1; num %3C= n - k + 1; num++) %7B%0A                List%3CInteger%3E list = new ArrayList%3C%3E();%0A                list.add(num);%0A      
     
@@ -546,34 +546,32 @@
                 
-  
 backtracking(k, 
@@ -571,45 +571,52 @@
 ing(
-k, 0, nums, new ArrayList(), result);
+list, k - 1, num + 1, n, ans);%0A            %7D
 %0A   
@@ -631,22 +631,19 @@
  return 
-result
+ans
 ;%0A      
@@ -655,16 +655,24 @@
         
+private 
 void bac
@@ -681,16 +681,36 @@
 racking(
+List%3CInteger%3E list, 
 int k, i
@@ -726,35 +726,14 @@
  int
-%5B%5D nums, List%3CInteger%3E curr
+ limit
 , Li
@@ -750,22 +750,19 @@
 teger%3E%3E 
-result
+ans
 ) %7B%0A    
@@ -777,24 +777,14 @@
 if (
-curr.size()
+k
  == 
-k
+0
 ) %7B%0A
@@ -799,22 +799,19 @@
         
-result
+ans
 .add(new
@@ -820,21 +820,23 @@
 rrayList
-(curr
+%3C%3E(list
 ));%0A    
@@ -847,43 +847,34 @@
     
-%7D else if (curr.size() %3C k) %7B%0A
+    return;%0A       
      
+%7D%0A
     
@@ -872,32 +872,33 @@
    %7D%0A           
+ 
 for (int i = sta
@@ -890,17 +890,19 @@
 or (int 
-i
+num
  = start
@@ -907,26 +907,25 @@
 rt; 
-i %3C nums.length; i
+num %3C= limit; num
 ++) 
@@ -946,24 +946,20 @@
     
-    curr
+list
 .add(num
 s%5Bi%5D
@@ -958,12 +958,8 @@
 (num
-s%5Bi%5D
 );%0A 
@@ -965,36 +965,32 @@
                 
-  
-  
 backtracking(k, 
@@ -990,42 +990,43 @@
 ing(
-k, i + 1, nums, curr, result);%0A   
+list, k - 1, num + 1, limit, ans);%0A
     
@@ -1041,21 +1041,20 @@
     
- curr
+list
 .remove(
 curr
@@ -1053,12 +1053,12 @@
 ove(
-curr
+list
 .siz
@@ -1071,26 +1071,8 @@
 1);%0A
-                %7D%0A
     

refactor 77