整体思路同之前的一样,依然采取降低维度的方式进行

public List<List<Integer>> solution(int nums[], int target) {
List<List<Integer>> result = new ArrayList<>();
if((nums.length<4)||(nums==null))
{
return result;
}
Arrays.sort(nums);
if ((4*nums[0]>target)||(4*nums[nums.length-1]<target))
{
return result;
}//上面两个队特例的快速结束一定要加
int N = nums.length;
for (int i = 0; i < N; i++) {
int[] temnum = new int[N - 1];
System.arraycopy(nums, 0, temnum, 0, i);
System.arraycopy(nums, i + 1, temnum, i, N - i - 1);
result = threeSum(temnum, target - nums[i], nums[i], result);
}
return result;
} public List<List<Integer>> threeSum(int[] nums, int target, int num,List<List<Integer>> result) {
int sum;
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1, end = nums.length - 1;
while (start < end) {
sum = nums[i] + nums[start] + nums[end];
if (sum < target) {
start++;
continue;
}
if (sum > target) {
end--;
continue;
}
//下面的部分为对每一结果内部进行进行排序,这样在去重时方便
if (sum == target) {
List<Integer> list = new ArrayList<>();
if(num<nums[i])
list.add(num);
list.add(nums[i]);
if(num>=nums[i] && num<nums[start])
list.add(num);
list.add(nums[start]);
if ( num>=nums[start] &&num<nums[end])
list.add(num);
list.add(nums[end]);
if (num>=nums[end])
list.add(num);
start++;
end--;
if (result.contains(list))
continue;
result.add(list);
}
}
}
return result;
}

整体速度在leetcode上大概为50%

leetcode 日记 4sum java的更多相关文章

  1. leetcode 日记 3sumclosest java

    思路一为遍历: public int thirdSolution(int[] nums, int target) { int result = nums[0] + nums[1] + nums[2]; ...

  2. leetcode 18 4Sum JAVA

    题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...

  3. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  4. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  6. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  7. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  8. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  9. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

随机推荐

  1. bootstrap笔记

    一.栅格系统:<div class="container">内容</div>固定宽度,1200px-margin==1170px<div class= ...

  2. Php中的强制转换详解

    强制转换中分为两种,第一种就只临时转换,和永久转换.在临时转换中呢,首先可以通过第一中方式来显示,就是小括号的形式,临时转换成整型我们可以通过(int)都是这样的形式,或者是(integer)临时转换 ...

  3. json_encode 中文乱码

    用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似"\u***"的格式, 还会在一定程度上增加传输的数据量. 而在PHP5.4, 这个问题终 ...

  4. Ruby--学习记录(实时更新)

    变量的命名方式决定了变量的种类: 局部变量  以英文字母或者_开头: 全局变量  以$开头: 实例变量  以@开头: 类变量     以@@开头:

  5. 【腾讯云的1001种玩法】在腾讯云上创建您的SQL Cluster(5)

    版权声明:本文由李斯达 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/97264001482830465 来源:腾云阁 h ...

  6. 严重:Error listenerStart

    转自: http://1985wanggang.blog.163.com/blog/static/7763833200942611050436/ 近日浏览论坛,发现好多人提问,都说在运行web程序时, ...

  7. haligong2016

    A 采用递推的方法,由于要到达棋盘上的一个点,只能从左边或者上边过来,根据加法原则,到达某一点的路径数目,就等于到达其相邻的上点和左点的路径数目的总和.所有海盗能达到的点将其路径数置为0即可. #in ...

  8. nodejs 服务端添加相应头Access-Control-Allow-Origin

    重点在这句:res.setHeader("Access-Control-Allow-Origin", "*"); var http = require(&quo ...

  9. Android 6编译环境搭建 (Marshmallow)

    1.安装 ubuntu 14.03 尽管android推荐 ubuntu 15, 安全起见,还是装LTS的14.04,步骤跳过 2. JDK: Marshmallow 需要 JDK8 ,添个源,顺手配 ...

  10. 提高ASP.NET应用程序性能的十大方法

    一.返回多个数据集 检查你的访问数据库的代码,看是否存在着要返回多次的请求.每次往返降低了你的应用程序的每秒能够响应请求的次数.通过在单个数据库请求中返回多个结果集,可以减少与数据库通信的时间,使你的 ...