leetcode 18. 4Sum——better way

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

//Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
//Find all unique quadruplets in the array which gives the sum of target.
//Note:
//Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
//The solution set must not contain duplicate quadruplets.
//For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
//A solution set is:
//    (-1,  0, 0, 1)
//    (-2, -1, 1, 2)
//    (-2,  0, 0, 2)

public class Solution {

	public static void main(String[] args) {
		int[] a = {-1,2,2,-5,0,-1,4};
		List<List<Integer>> result = fourSum(a,3);
		for(int i = 0;i<result.size();i++){
			System.out.println(result.get(i));
		}
	}

	public static List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        HashSet<List<Integer>> contrast = new HashSet<List<Integer>>();
        Arrays.sort(nums);
        for(int i = 0;i<nums.length;i++){										//与3sum相似,先确定第一个元素
        	for(int j = i+1;j<nums.length;j++){									//确定第二个元素
        		int k = j+1;													//后两个元素取之后总体元素的两端
        		int l = nums.length-1;
        		while(k<l){
            		int sum = nums[i]+nums[j]+nums[k]+nums[l];
        			if(sum>target){												//根据sum与target的比较确定l和k如何移动
            			l--;
            		}else if(sum<target){
            			k++;
            		}else if(sum == target){
            			List<Integer> temp = new ArrayList<Integer>();
            			temp.add(nums[i]);
            			temp.add(nums[j]);
            			temp.add(nums[k]);
            			temp.add(nums[l]);
            			if(!contrast.contains(temp)){								//查看result中是否存在
            				contrast.add(temp);
            				result.add(temp);
            			}
            			k++;
            			l--;
            		}
        		}
        	}
        }
        return result;
    }

}

时间: 2024-08-30 02:27:33

leetcode 18. 4Sum——better way的相关文章

LeetCode 18. 4Sum (四数之和)

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For exampl

[LeetCode] 18. 4Sum ☆☆

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For exampl

LeetCode——18. 4Sum

一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的. 三.题解: 这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或

LeetCode 18 4Sum(4个数的和)

翻译 给定一个有n个数字的数组S,在S中是否存在元素a,b,c和d的和恰好满足a + b + c + d = target. 找出数组中所有的不想等的这四个元素,其和等于target. 备注: 在(a,b,c,d)中的元素必须从小到大排列.(a ≤ b ≤ c ≤ d) 其结果必须不能够重复. 例如,给定S = {1 0 -1 0 -2 2},target = 0. 一个结果集为: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2) 原文 Given an ar

leetCode 18.4Sum (4数字和) 解题思路和方法

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending o

LeetCode 18. 4Sum new

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For exampl

Array + two points leetcode.18 - 4Sum

题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b+ c + d = target? Find all unique quadruplets in the array which gives the sum of target. 给定无序数组,找到和为target的不重复的长度为4的子序列 样例 1. Given

[leetcode 18]4Sum

1 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending

LeetCode #18 4Sum (M)

[Problem] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descend

Java [leetcode 18]4Sum

问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending