题目

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

解题思路

将nums数组重新排列,然后从i = 0开始遍历,分别取j = i + 1、left = j + 1和right = nums.length - 1,将nums[i]、nums[j]、nums[left]和nums[right]四数之和与target比较,如果相等则判断结果数组列表中是否存在,存在的话则添加。因为不包含重复的四元组,所以通过一些除重操作减少运行次数。

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length - 3;i++)
{
if(i > 0 && nums[i] == nums[i - 1]) //nums[i]与nums[i-1]相同时,则可直接跳过,判断nums[i+1],减少运行次数
continue;
for(int j = i + 1; j < nums.length - 2;j++)
{
if(j > i + 1 && nums[j] == nums[j - 1])
continue;
int left = j + 1;
int right = nums.length -1;
while(left < right)
{
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum == target)
{
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[left]);
temp.add(nums[right]);
if(!list.contains(temp))
list.add(temp);
right--;
left++;
}
else if(sum > target)
right--;
else
left++;
}
}
}
return list;
}
}

  

leetcode 18 4Sum JAVA的更多相关文章

  1. 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 ...

  2. [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 = tar ...

  3. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  4. 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 = tar ...

  5. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  6. [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 = tar ...

  7. leetcode 日记 4sum java

    整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...

  8. 18. 4Sum (JAVA)

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  9. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

随机推荐

  1. python时间戳、日期、时间转换

    1.str转时间戳 # 字符类型的时间 tss1 = '2013-10-10 23:40:00' # 转为时间数组 timeArray = time.strptime(tss1, "%Y-% ...

  2. 产品负责人(Product Owner)的主要职责和技能

    角色介绍 产品负责人以下简称PO,他是有授权的产品领导力核心,组成Scrum团队三个角色之一. PO担任的是产品经理的角色. PO的主要职责 1.对产品的ROI负责. ROI = profitabil ...

  3. 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】

    题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...

  4. 值得一做》关于一道暴搜BZOJ1024(EASY+)

    为什么要写这道题的DP捏? 原因很简单,因为为原来在openjudge上有一道题叫分蛋糕,有一个思路和这道题很像:“分锅”. 分锅:即为考虑计算当前情况的最优解时,把当前状态结果,分散为考虑当前状态的 ...

  5. Node.js简介(转)

    目前,Node.js是在前端页面开发中十分受欢迎的,它是一套用来编写高性能网络服务器的JavaScript工具包,在本文中,将带领各位初学者介绍Node JS的基本知识,要求本文的阅读对象为有一定Ja ...

  6. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  7. web.xml配置详解[转]

    引文: 对于一个J2EE领域的程序员而言,基本上每天都会和web应用打交道. 什么是web应用?最简单的web应用什么样?给你一个web应用你该从何入手? 1.什么是web应用? web应用是一种可以 ...

  8. win7 + eclipse + cocos2dx 开发环境配置

    最近想在win7上配置eclipse+cocos2dx开发环境,在安装之前一定要注意每项是32位还是64位,我选择的都是64位版本的,闲话少叙我们开始安装吧! 1.下载cocos2dx,我选择的是co ...

  9. code1074 食物链

    开3*n的并查集,其中x用来连接与x同类的,x+n用来连接x吃的,x+2*n用来连接x被吃的. 1 x y时,如果 x吃y 或 x被y吃,那么为假话, 否则x与y同类,x吃的y也吃,x被吃的y也被吃: ...

  10. turntable

    1.业务流程 2.80001代码逻辑 3.80002代码逻辑 4抽奖概率计算