what is the time complexity for this codes


  • 0
    O

    '''
    public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
    HashMap<Integer, List<List<Integer>>> map = new HashMap<>();
    for(int i = 0; i < nums.length; i++){
    for(int j = i + 1; j < nums.length; j++){
    int sum = nums[i] + nums[j];
    if(!map.containsKey(sum)){
    map.put(sum, new ArrayList<>());
    }
    map.get(sum).add(Arrays.asList(i, j));
    }
    }
    HashSet<List<Integer>> set = new HashSet<>();
    HashSet<Integer> set1 = new HashSet<>();
    for(int i = 0; i < nums.length; i++) {
    if (set1.contains(nums[i])){
    continue;
    }else{
    set1.add(nums[i]);
    }
    HashSet<Integer> set2 = new HashSet<>();
    for(int j = i + 1; j < nums.length; j++){
    if (set2.contains(nums[j])){
    continue;
    }else{
    set2.add(nums[j]);
    }
    if(map.containsKey(target-nums[i]-nums[j])){
    List<List<Integer>> twoSum = map.get(target-nums[i]-nums[j]);
    for (List<Integer> list : twoSum) {
    if(list.get(0) == i || list.get(1) == j){
    continue;
    }
    if(list.get(0) == j || list.get(1) == i){
    continue;
    }
    List<Integer> l = new ArrayList<>();
    l.addAll(Arrays.asList(nums[i], nums[j], nums[list.get(0)],nums[list.get(1)]));
    Collections.sort(l);
    set.add(l);
    }
    }
    }
    }
    List<List<Integer>> res = new ArrayList<>(set);
    Collections.sort(res, new Comparator<List<Integer>>(){
    public int compare(List<Integer> list1, List<Integer> list2){
    if(list1.get(0) == list2.get(0)){
    if(list1.get(1) == list2.get(1)){
    return list1.get(2) - list2.get(2);
    }else{
    return list1.get(1) - list2.get(1);
    }
    }else{
    return list1.get(0) - list2.get(0);
    }
    }
    });
    return res;
    }
    }
    '''


Log in to reply
 

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