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 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)
解题思路:
与之前的3sum相类似,只不过在外边多了一套循环,时间复杂度为O(n^3)
代码如下:
public class Solution {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> fourSum(int[] nums, int target) {
int length = nums.length;
int tmp;
if (nums == null || length < 4)
return ans;
Arrays.sort(nums);
for (int i = 0; i < length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1])
continue;
int begin = j + 1;
int end = length - 1;
while (begin < end) {
tmp = nums[begin] + nums[end] + nums[i] + nums[j];
if (tmp == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[begin]);
list.add(nums[end]);
ans.add(list);
while (begin < end && nums[begin + 1] == nums[begin])
begin++;
begin++;
while (begin < end && nums[end - 1] == nums[end])
end--;
end--;
} else if (tmp > target)
end--;
else
begin++;
}
}
}
return ans;
}
}
Java [leetcode 18]4Sum的更多相关文章
- leetcode 18 4Sum JAVA
题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...
- [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 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- 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 ...
- C#解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 ...
随机推荐
- 手把手教你写LKM rookit! 之 杀不死的pid&root后门
......上一节,我们编写了一个基本的lkm模块,从功能上来说它还没有rootkit的特征,这次我们给它添加一点有意思的功能.我们让一个指定的进程杀不死, 曾经,想写一个谁也杀不死的进程,进程能捕捉 ...
- Linux进程间通信IPC学习笔记之管道
基础知识: 管道是最初的Unix IPC形式,可追溯到1973年的Unix第3版.使用其应注意两点: 1)没有名字: 2)用于共同祖先间的进程通信: 3)读写操作用read和write函数 #incl ...
- 1065. A+B and C (64bit)
#include<stdio.h> #include <math.h> int main() { long long a,b,c,sum; int n,i; while(sca ...
- MQ 2035(MQRC_NOT_AUTHORIZED)
当使用MQ7.1或7.5时,如果使用MQ管理员账号去连接MQ服务器,可能会报以下的错误,提示你权限不足. 2035 MQRC_NOT_AUTHORIZED 在之前的版本中是没有这个问题的. 原因是在7 ...
- IIS7 无法访问请求的页面,因为该页的相关配置数据无效
HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 解决方案: C:\Windows\Microsoft.NET\Frame ...
- TWaver初学实战——如何在TWaver属性表中添加日历控件?
在日期输入框中添加日历控件,是一种非常流行和实用的做法.临渊羡鱼不如退而写代码,今天就看看在TWaver中是如何实现的. 资源准备 TWaver的在线使用文档中,就有TWaver Proper ...
- 企业应用的Web程序的安全性
提起安全性这个话题,大家恐怕依稀还记得Sony的PSP账户信息泄露的事故造成的重大损失.但是又隐隐觉得这事儿离我很远,无需过多考虑.也有的人会想,我们做的是企业内部系统所以不必太在意.但是,Web程序 ...
- java第三课:分支结构、循环结构
分支结构 1.if后面可以不加{},但是不建议,容易产生误解2.写程序的时候先防屌丝测试,再写逻辑,否则会出错3.if:一条分支 if else:两条分支 if...else if...:多条分支(结 ...
- springmvc 数据对象回绑
springmvc中,由页面 post到 controller,对象可以在form里面设置modelAttribute达到回绑的目的. 但是如果对象里面有复杂的非String,int的对象,则要在co ...
- uva 165
回溯 参考了一下别人的解法 1 必须存在 再枚举下一个数字的时候 从当前可取到的最小数字加一枚举到当前可取到的最大数字加一 /********************************* ...