Is there anyone could tell me what the time complexity of my solution is? O(n^2)? It passed in the OJ.


  • 2
    E

    public class Solution {
    public List<List<Integer>> threeSum(int[] num) {
    Arrays.sort(num);
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    helper(num, 0, new ArrayList<Integer>(), result, 0);
    return result;
    }

    private void helper(int [] num, int start, List<Integer> current, List<List<Integer>> result, int left) {
        if(current.size() == 3) {
            if(left == 0) {
                result.add(new ArrayList<Integer>(current));
            }
            return;
        }
        for(int i = start; i < num.length; i++) {
            if(i > start && num[i] ==  num[i - 1]) {
                continue;
            }
            current.add(num[i]);
            helper(num, i + 1, current, result, left - num[i]);
            current.remove(current.size() - 1);
        }
    }
    

    }


  • 0
    B

    Could you tell me why you have:

    if(i > start && num[i] == num[i - 1]) {
    continue;
    }


Log in to reply
 

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