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

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. 有关uploadifive的使用经验

    这段时间做了一个项目用到uploadifive上传控件,和uploadify不同,这个控件是基于HTML5的版本而不用支持falsh,因而移动端也可以使用. 整理用过的相关属性与方法: 属性 作用 a ...

  2. eclipse绘制activiti无法生成图形

    今天使用eclipse绘制acitiviti,发现无法绘制图形,设置里面勾选也勾选了,同时activiti插件也是最新的,最后发现同时打开了两个eclipse窗口就不支持生成图形,看来只能开一个窗口了 ...

  3. understanding ECMAscript 6 ---- block bindings

    Traditionally, the way variable declarations work has been one tricky part of programming in javascr ...

  4. <mvc:annotation-driven/>与<context:annotation-config/>的区别

    在使用注解的方式配置SSM的时候一般会配置<mvc:annotation-driven/>与<context:annotation-config/>,有时候会对两者的概念有些区 ...

  5. HTML5 十大新特性(五)——SVG绘图

    相对于canvas绘图,SVG是一种绘制矢量图的技术.全称叫做Scalable Vector Graphics,可缩放的矢量图,在2000年就已经存在,H5把它纳入了标准标签库,并进行了一些瘦身.需要 ...

  6. Unity3d调用iOS陀螺仪

    How to write gyroscope controller with Unity3d http://blog.heyworks.com/how-to-write-gyroscope-contr ...

  7. js判断数据类型

    1.判断一个数字是否是无穷的 isFinite()例:var aa=Number.POSITIVE_INFINITY; if(isFinite(aa)){ alert("aa不是无穷的&qu ...

  8. call 和 apply使用

      call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向.因为 JavaScript 的函数存在「定义时上下文」 ...

  9. FileFilter 遍历某个目录下文件名含有某个字符的文件

    由于IIS版本的升级,造成了文件名中含有“+”的特殊字符的文件(多数是图片)在网页中不能被访问,于是必须查找当前目录下含有多少这样的文件,从而制定最佳的解决方案. 废话少说,直接上核心代码: publ ...

  10. Java—数组

    1 声明数组变量   dataType[] arrayRefVar; 2 实例数组         double[] myArray; 3 创建数组         arrayRefVar =new ...