java 18. 4Sum(#)。java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 18. 4Sum(#)。java相关的知识,希望对你有一定的参考价值。

public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> outlist = new ArrayList<>();
        HashMap<Integer, ArrayList<int[]>> map = new HashMap<>();
        HashSet<String> set = new HashSet<>();
        StringBuffer sb = new StringBuffer();
        Arrays.sort(nums);//NlogN
        for(int i=0;i<nums.length-1;i++){//N*N
            for(int j=i+1;j<nums.length;j++){
                int sum = nums[i]+nums[j];
                ArrayList list = map.getOrDefault(sum, new ArrayList<int[]>());
                list.add(new int[]{i,j});
                map.put(sum,list);
            }
        }

        for(Integer sum: map.keySet()){
            int companion = target-sum;
            if(map.containsKey(companion)){
                ArrayList<int[]> l1 = map.get(sum);
                ArrayList<int[]> l2 = map.get(companion);
                for(int[] arr1: l1){
                    for(int[] arr2: l2){
                        if(arr2[0]>arr1[1]){
                            String str = sb.append(nums[arr1[0]]).append(nums[arr1[1]])
                                    .append(nums[arr2[0]]).append(nums[arr2[1]]).toString();
                            if(!set.contains(str)) {
                                set.add(str);
                                outlist.add(Arrays.asList(nums[arr1[0]], nums[arr1[1]], nums[arr2[0]], nums[arr2[1]]));
                            }
                            sb.delete(0,sb.length());
                        }
                    }
                }
            }
        }
        return outlist;
    }
public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
    ArrayList<List<Integer>> ans = new ArrayList<>();
    if(num.length<4)return ans;
    Arrays.sort(num);
    for(int i=0; i<num.length-3; i++){
        if(num[i]+num[i+1]+num[i+2]+num[i+3]>target)break; //first candidate too large, search finished
        if(num[i]+num[num.length-1]+num[num.length-2]+num[num.length-3]<target)continue; //first candidate too small
        if(i>0&&num[i]==num[i-1])continue; //prevents duplicate result in ans list
        for(int j=i+1; j<num.length-2; j++){
            if(num[i]+num[j]+num[j+1]+num[j+2]>target)break; //second candidate too large
            if(num[i]+num[j]+num[num.length-1]+num[num.length-2]<target)continue; //second candidate too small
            if(j>i+1&&num[j]==num[j-1])continue; //prevents duplicate results in ans list
            int low=j+1, high=num.length-1;
            while(low<high){
                int sum=num[i]+num[j]+num[low]+num[high];
                if(sum==target){
                    ans.add(Arrays.asList(num[i], num[j], num[low], num[high]));
                    while(low<high&&num[low]==num[low+1])low++; //skipping over duplicate on low
                    while(low<high&&num[high]==num[high-1])high--; //skipping over duplicate on high
                    low++; 
                    high--;
                }
                //move window
                else if(sum<target)low++; 
                else high--;
            }
        }
    }
    return ans;
}}
public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> l = new LinkedList<>();
        int lo = 0;
        int hi = nums.length - 1;
        int sum = 0;
        for(int i = 0; i < nums.length-3; i++) {
            if(nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target) break;
            if(nums[i]+nums[nums.length-1]+nums[nums.length-2]+nums[nums.length-3]<target) continue; 
            if(i > 0 && nums[i] == nums[i-1]) continue;
                for(int j = i+1; j < nums.length-2; j++) {
                    if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
                    lo = j+1;
                    hi = nums.length-1;
                    if(j>i+1 && nums[j]==nums[j-1]) continue;
                    while(lo < hi) {
                        sum = nums[i] + nums[j]+ nums[lo] + nums[hi];
                        if(sum == target) {
                            l.add(Arrays.asList(nums[i], nums[j], nums[lo], nums[hi]));
                            while(lo < hi && nums[lo] == nums[lo+1]) lo++;
                            while(lo < hi && nums[hi] == nums[hi-1]) hi--;
                            lo++; hi--;
                        } else if(sum < target) {
                            lo++;
                        } else {
                            hi--;
                        }
                    }
                }
                
            
            
        }
        return l;

    }
}
public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new LinkedList<>();
        if (nums.length < 4) return res;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 3; i++){
            if (i > 0 && nums[i-1] == nums[i]) {
                continue;
            }
            for (int j = i + 1; j < nums.length - 2; j++){
                if ( j > i + 1 && nums[j-1] == nums[j]) {
                    continue;
                }
                int k = j + 1;
                int l = nums.length - 1;
                while ( k < l){
                    if ( k > j + 1 && nums[k-1] == nums[k]) {
                        k++;
                        continue;
                    }
                    int tempSum = nums[i] + nums[j] + nums[k] + nums[l];
                    if (tempSum == target) {
                        res.add(Arrays.asList(nums[i],nums[j],nums[k],nums[l]));
                        k++;
                        l--;
                    } else if (tempSum > target) {
                        l--;
                    } else {
                        k++;
                    }
                    
                }//while loop
            }//for loop index j
        }//for loop index i
        return res;
    }
}

以上是关于java 18. 4Sum(#)。java的主要内容,如果未能解决你的问题,请参考以下文章

java 18. 4Sum(#)。java

java 18. 4Sum(#)。java

java 18. 4Sum(#)。java

java 18. 4Sum(#)。java

leetcode 18 4Sum JAVA

[LeetCode][Java] 4Sum