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 ...
随机推荐
- 从零开始搭建k8s-20180301
yum install -y yum-utils git etcd yum-config-manager --add-repo https://download.docker.com/linux/ce ...
- Scala基础:模式匹配和样例类
模式匹配 package com.zy.scala import scala.util.Random /** * 模式匹配 */ object CaseDemo { def main(args: Ar ...
- ArcGIS js api开发环境配置
转自https://blog.csdn.net/lovecarpenter/article/details/53713481#3%E9%85%8D%E7%BD%AEarcgis-api%E5%AE%9 ...
- th:onclik()传参问题(前端使用了bootstrap)
网上大多帖子是这么写的 onclick调javascript函数时,不能直接使用onclick=“editUser(${prod.id})”,这样会报错,需要修改成如下的格式. <a href= ...
- ubuntu14.04 64位安装 g2o
参考链接:http://blog.csdn.net/jiujiu932/article/details/52248577 http://www.cnblogs.com/gaoxiang12/p/473 ...
- linux常用的一些命令行操作(ubuntu)
软件安装 sudo apt-get install xxx 压缩和解压缩 1. *.tar 用 tar –xvf 解压 2. *.gz 用 gzip -d或者gunzip 解压 3. *.tar.gz ...
- Python String模块详解
>>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL ...
- p4051 [JSOI2007]字符加密
传送门 分析 将字符串复制一遍然后直接求sa即可 代码 #include<iostream> #include<cstdio> #include<cstring> ...
- Storm中并行度原来是这样计算的(1.0.1版本)
==思考问题1== 向集群提交一个拓扑的时候,Storm是如何计算Task数以及Executor数的? 具体有多少个worker,多少个executor,每个executor负责多少个task? == ...
- bash 环境配置及脚本
bash是 Bourne Again Shell简称 ,从unix系统的sh发展而来 查看当前shellecho $SHELL查看系统支持的shellcat /etc/shells cd /binls ...