Given an array S of n integers, are there elements abc, 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)

思路:

讨论里有说O(N2)的解法,好像是分为两个2 Sum来做。没仔细看。

下面贴个常规O(n3)解法,注意去重。

vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int>> ans;
if(num.size() < ) return ans;
int l1, l2, l3, l4;
sort(num.begin(), num.end());
for(l1 = ; l1 < num.size() - ; l1++)
{
if(l1 > && num[l1] == num[l1 - ]) continue; //注意去重复方法 含义是在另外三个数字固定的情况下 同一位置的数字不能重复
for(l4 = num.size() - ; l4 >= l1 + ; l4--)
{
if(l4 < num.size() - && num[l4] == num[l4 + ]) continue;
l2 = l1 + ;
l3 = l4 - ;
while(l2 < l3)
{
if(num[l1]+num[l2]+num[l3]+num[l4] == target)
{
vector<int> partans;
partans.push_back(num[l1]);
partans.push_back(num[l2]);
partans.push_back(num[l3]);
partans.push_back(num[l4]);
ans.push_back(partans); l2++;
while(num[l2] == num[l2 - ])
{
l2++;
}
l3--;
while(num[l3] == num[l3 + ])
{
l3--;
}
}
else if(num[l1]+num[l2]+num[l3]+num[l4] < target)
{
l2++;
}
else
{
l3--;
}
}
}
}
return ans;
}

大神看起来更统一的代码:

public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
List<List<Integer>> results = new LinkedList<List<Integer>>();
if (num == null || num.length < )
return results;
Arrays.sort(num); for (int s = ; s < num.length - ; s++) {
if (s > && num[s] == num[s - ]) continue; for (int e = num.length - ; e >= s + ; e--) {
if (e < num.length - && num[e] == num[e + ]) continue; int local = target - num[s] - num[e];
int j = s + ;
int k = e - ;
while (j < k) { if (j > s + && num[j] == num[j - ]) {
j++;
continue;
}
if (k < e - && num[k] == num[k + ]) {
k--;
continue;
} if ((num[j] + num[k]) > local)
k--;
else if ((num[j] + num[k]) < local)
j++;
else
results.add(new ArrayList<Integer>(Arrays.asList(
num[s], num[j++], num[k--], num[e])));
}
}
}
return results;
}
}

【leetcode】4Sum(middle)的更多相关文章

  1. 【leetcode】Candy(python)

    题目要求的比它的邻居比自己奖励,因此,我们有最少一个多的.所有我们可以找到所有的坑,凹坑例如,存在以下三种情况. 找到全部的凹点后,我们就能够从凹点处開始向左右两个方向依次查找递增序列.当中每一个高的 ...

  2. NoSQL之【MongoDB】学习(三):配置文件说明

    摘要: 继上一篇NoSQL之[MongoDB]学习(一):安装说明 之后,知道了如何安装和启动MongoDB,现在对启动时指定的配置文件(mongodb.conf)进行说明,详情请见官方. 启动Mon ...

  3. 【操作系统】进程间通信(C#)

    原文:[操作系统]进程间通信(C#) 08年9月入学,12年7月毕业,结束了我在软件学院愉快丰富的大学生活.此系列是对四年专业课程学习的回顾,索引参见:http://blog.csdn.net/xia ...

  4. 【Luogu3444】ORK-Ploughing(贪心)

    [Luogu3444]ORK-Ploughing(贪心) 题面 Luogu 题解 我们知道,如果我们选定了以横向为主,或者纵向为主, 那么就有尽可能减少另一个方向上耕地的次数 所以分开贪心,但是本质相 ...

  5. 【BZOJ1997】Planar(2-sat)

    [BZOJ1997]Planar(2-sat) 题面 BZOJ 题解 很久没做过\(2-sat\)了 今天一见,很果断的就来切 这题不难呀 但是有个玄学问题: 平面图的性质:边数\(m\)的最大值为\ ...

  6. 【Luogu1337】平衡点(模拟退火)

    [Luogu1337]平衡点(模拟退火) 题面 洛谷 题解 和BZOJ3680吊打XXX是一样的.. 但是数据很强呀.. 疯狂调参 各种WA... 很无奈呀.... #include<iostr ...

  7. 【BZOJ1996】合唱队(动态规划)

    [BZOJ1996]合唱队(动态规划) 题面 BZOJ 题解 很容易的一道题 因为每个人不是放在了左边就是放在了右边 所以每次放好的人必定是原序列的一个子串 所以,很容易想到区间\(dp\) 设\(f ...

  8. 【BZOJ1899】午餐(动态规划)

    [BZOJ1899]午餐(动态规划) 题面 BZOJ 题解 我太弱了 这种\(dp\)完全做不动.. 首先,感性理解一些 如果所有人都要早点走, 那么,吃饭时间长的就先吃 吃饭时间短的就晚点吃 所以, ...

  9. 【BZOJ1040】骑士(动态规划)

    [BZOJ1040]骑士(动态规划) 题面 BZOJ 题解 对于每一组厌恶的关系 显然是连边操作 如果是一棵树的话 很显然的树型\(dp\) 但是,现在相当于有很多个基环 也就是在一棵树的基础上再加了 ...

随机推荐

  1. CI 更新字段

    function update_click_num($brand_id) { $this->db->set('click_num', 'click_num+1', FALSE); $thi ...

  2. windows下cmd命令行显示UTF8字符设置(CHCP命令)

    本文由 www.169it.com 收集整理 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下 ...

  3. python的全局变量玩法还挺特别的

    global g_arr def add(): #global g_arr g_arr = [] g_arr.append(1) add() print g_arr #你将收获一个NameError错 ...

  4. 【转】Android属性之build.prop,及property_get/property_set

    简要分析一下build.prop是如何生成的.Android的build.prop文件是在Android编译时收集的各种property(LCD density/语言/编译时间, etc.),编译完成 ...

  5. c# using 引用和别名的使用

    1.使用别名 在同时引用的两个命名空间中有相同的类型时,可以使用别名来区分.如下所示: using System; using System.Threading; using System.Timer ...

  6. JS面向对象5中写法

    //定义Circle类,拥有成员变量r,常量PI和计算面积的成员函数area() //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14 ...

  7. 使用PSD设计网页页面

    一.一个独立的页面 1.分析这个页面,在脑海或草稿上要确立页面板块布局(如版块区域的,位置和大小)2.根据设计稿的的情况,分析背景图的分布.ICO图的分布等 3.切割相应的图片,导出.合并图片(一般用 ...

  8. javascript笔记——cookie解析

    JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...

  9. C# Winform程序打包

    1.新建一个安装项目,起名“Setup3” 2.点击应用程序文件夹 3.在右侧右键点击添加文件 注意:是Debug文件夹下所有程序 接着在添加你的应用程序项目的时候,多添加一个msiexec.exe进 ...

  10. 短租app简析

    本人应聘某短租app产品经理时做的材料,贴出来请高手指教. 所有内容来自公开资料,不涉及商业秘密.