Help !!! can not pass this code !! said time limited

• ``````public class Solution {
List<List<Integer>> result;
List<Integer> list;
public List<List<Integer>> combine(int n, int k) {
result = new ArrayList<List<Integer>>();
if(n < k)
return result;
list = new ArrayList<Integer>();
helper(n, k);
return result;
}

private void helper(int n, int k)    {
if(k == 0)  {
return;
}

int  len = list.size();
for(int i = len + 1; i <= n; i++)  {
helper(n, k - 1);
list.remove(len);
}

return;
}
``````

}

• Hi miceteat,

You have some bugs. Below is solution based on yours.

``````public class Solution {
List<List<Integer>> result;
List<Integer> list;
public List<List<Integer>> combine(int n, int k) {
result = new ArrayList<List<Integer>>();
if(n < k)
return result;
list = new ArrayList<Integer>();
helper(1, n, k);
return result;
}

private void helper(int start, int end, int k)    {
if(k == 0)  {
List<Integer> l = new ArrayList<Integer>();
return;
}

for(int i = start; i <= end; i++)  {
helper(i + 1, end, k - 1);
list.remove(list.size() - 1);
}

return;
}
}
``````

First bug:

``````    if(k == 0)  {
List<Integer> l = new ArrayList<Integer>();
return;
}
``````

Second bug:

list should remove the last element, not the len.

``````list.remove(list.size() - 1);
``````

Third bug:

``````    // Not from len + 1, but from list.last_value + 1
for(int i = len + 1; i <= n; i++)  {