Why my answer got "Time Limit exceeded"


  • 0
    S
    public class Solution {
    public List<List<Integer>> subsets(int[] S) {
        Arrays.sort(S);
        List<List<Integer>> l = new ArrayList<List<Integer>> ();
        l.add(new ArrayList<Integer>());
        sub(S, l, 0);
        return l;
        
        
    }
    public void sub(int[] S, List<List<Integer>> l, int start) {
        if (start == S.length)
            return;
        List<Integer> temp = new ArrayList<Integer>();
        sub (S, l, start+1);
        int i = start;
        int cur_size = l.size();
        for (int size = 0; size < cur_size; ++size) {
            temp = l.get(size);
            temp.add(0,S[i]);
            l.add(temp);
        }
    }
    

    }

    my idea is:
    number n subset = n - 1 subset + itself.
    So i use recursion, and then in for loop, I add the number n into the n-1 subsets.

    But i don't know why my answer exceeded limit time?
    Any helps appreciate.


Log in to reply
 

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