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 ...
随机推荐
- LeetCode Crack Note --- 1. Two Sum
Discription Given an array of integers, return indices of the two numbers such that they add up to a ...
- jar 运行报错:找不到或无法加载主类
NIFEST.MF文件中指定的,如下所示:Manifest-Version: 1.0Class-Path: .Main-Class: com.webex.app.Main // ...
- 将DataTable中的数据导出成Excel
public bool ExportFile(System.Data.DataTable dt){ SaveFileDialog sfd = new SaveFileDialog(); s ...
- 运行Junit方法项目启动不了
从控制台看不出任何有用信息,通过JUnit右键”Copy Failure List”将信息拷贝出来 TestStart.start initializationError(org.junit.runn ...
- Blending
[Blending] Blending is used to make transparent objects. When graphics are rendered, after all shade ...
- Unity3D中暂停时的动画及粒子效果实现
暂停是游戏中经常出现的功能,而Unity3D中对于暂停的处理并不是很理想.一般的做法是将Time.timeScale设置为0.Unity的文档中对于这种情况有以下描述: The scale at wh ...
- Ztree右键事件,如何让指定的子节点不显示右键菜单。
这里我记录一下我自己的解决方案: 1.首先在Ztree的setting设置中加一个鼠标右键回调函数onRightClick,然后在加一个beforeRightClick(具体含义可以看官方API) v ...
- RPM简介与基本使用
一.什么是RPM RPM的全名是Red Hat Package Manager,本意是Red Hat 软件包管理,顾名思义是Red Hat 贡献出来的软件包管理:在Fedora .Redhat.Man ...
- IOS操作系统上执行monkey测试
IOS操作系统上执行monkey测试 IOS操作系统不像Android系统那么方便,各种限制也比较多,目前我的建议还是直接在模拟器上执行monkey测试.如果需要在真机上面执行,可以参考文档: htt ...
- Python创建单例模式的5种常用方法-乾颐堂
所谓单例,是指一个类的实例从始至终只能被创建一次. 方法1 如果想使得某个类从始至终最多只有一个实例,使用__new__方法会很简单.Python中类是通过__new__来创建实例的: 1 2 3 4 ...