Why my program is time-limited? The size of ans is correct but the size of each element in ans is 0,why?

  • 0
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        return ans;
    public void Rec(List<List<Integer>> ans, List<Integer> temp, int n,
    		int k, int base) {
    	// TODO Auto-generated method stub
    	if (temp.size() == k) {
    	for (int i = base; i <= n; i++) {
    		Rec(ans, temp, n, k, i + 1);
    		temp.remove(temp.size() - 1);

  • 0

    Well, you're adding the same single list to the answer over and over again, and you always remove any item from it that you added. So of course it ends up empty. You should add copies to the answer instead, e.g.,

    ans.add(new ArrayList(temp));

    For your other question, why your program is time-limited, I don't have the slightest clue what that's supposed to mean.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.