leetcode 18 4Sum JAVA
题目
给定一个包含 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的更多相关文章
- 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 ...
- [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 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- 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 ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个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 ...
- leetcode 日记 4sum java
整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...
- 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 ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
随机推荐
- Dubbo限制大数据传输的解决方案
当service层向web层传输大数据容量的对象时,会受到Dubbo的限制,报类似如下异常: com.alibaba.dubbo.remoting.transport.AbstractCodec.ch ...
- jQuery——表单异步提交
如果不做任何处理,表单提交时会刷新页面,为了改善体验,可以使用jQuery做到异步提交表单:通过$("#form").serialize()将表单元素的数据转化为字符串,然后通过$ ...
- LeetCode之位操作题java
191. Number of 1 Bits Total Accepted: 87985 Total Submissions: 234407 Difficulty: Easy Write a funct ...
- AspectJ的aop编程--切入点表达式
- MySQL数据库篇之pymysql模块的使用
主要内容: 一.pymysql模块的使用 二.pymysq模块增删改查 1️⃣ pymsql模块的使用 1.前言:之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库, 那如何在p ...
- 批量判断网页是否NOT found
import java.net.HttpURLConnection;import java.net.URL; public class NetValible{ static String[] url ...
- python操作符重载
操作符重载的魔术方法 操作符 方法 取反 即席 + __add__ __radd__ __iadd__ - __sub__ __rsub__ __isub__ * __mul__ __rmul__ _ ...
- 【总结整理】自带天气app,为什么还要下载
很简单那就说明用户对天气这个功能的需求并没有表面那么简单呗,还有更深层次的需求~ 先声明我自己是没有这方面需求的,我就纯属YY一下 既然数据都一样的话,那是什么让用户觉得天气APP更专业呢? 1.历史 ...
- Lucas–Kanade光流算法学习
Lucas–Kanade光流算法是一种两帧差分的光流估计算法.它由Bruce D. Lucas 和 Takeo Kanade提出. 光流(Optical flow or optic f ...
- LoadRunner11学习记录四 -- 集合点
LoadRunner集合点的设置: 我们来想象一个场景,10名运动员参加长跑比赛,出发点同时起跑,他们是并排奔跑的:跑了N圈之后,因为有体能更强的,有体能稍弱的,他们的队形并排变成了前后.几乎一个跑道 ...