4Sum

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)
与3Sum类似,只是我们现在需要遍历两个元素,a,b,然后在利用两个指针的方法求c,d
 
 class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) { int n=num.size();
int i,j,k,l;
int a,b,c,d;
sort(num.begin(),num.end());
vector<vector<int> > result;
for(int i=;i<n-;i++)
{
for(int j=i+;j<n-;j++)
{
k=j+;
l=n-; while(k<l)
{
a=num[i];
if(i>&&num[i]==num[i-])
{
break;
} b=num[j];
if(j>i+&&num[j]==num[j-])
{
break;
} c=num[k];
if(k>j+&&num[k]==num[k-])
{
k++;
continue;
} d=num[l];
if(l<n-&&num[l]==num[l+])
{
l--;
continue;
} int sum=a+b+c+d; if(sum<target)
{
k++;
}
else if(sum>target)
{
l--;
}
else
{
vector<int> tmp();
tmp[]=a;
tmp[]=b;
tmp[]=c;
tmp[]=d;
result.push_back(tmp);
k++;
l--;
}
}
}
}
return result;
}
};

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

  1. 【LeetCode】4Sum 解题报告

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  2. 【leetcode】4Sum(middle)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  3. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  4. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  5. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  6. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  7. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. Qt无边框,可移动窗口

    QPoint dragPosition; void MainWindow::mousePressEvent(QMouseEvent *event) { if(event->button()==Q ...

  2. A星寻路算法

    A星寻路算法 1.准备一个close关闭列表(存放已被检索的点),一个open开启列表(存放未被检索的点),一个当前点的对象cur 2.将cur设成开始点 3.从cur起,将cur点放入close表中 ...

  3. C# 中的多线程

    参考网站http://blog.gkarch.com/topic/threading.html

  4. poj 1743 二分答案+后缀数组 求不重叠的最长重复子串

    题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3  or  子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...

  5. WDCP(WDlinux Control Panel) mysql/add_user.php、mysql/add_db.php Authentication Loss

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 http://www.wooyun.org/bugs/wooyun-2010-06 ...

  6. Spring监听器配置

    使用spring框架时如果同时使用org.springframework.web.util.Log4jConfigListener监听器,那么在web.xml中的监听器的注册顺序为org.spring ...

  7. udp内网穿透 两个内网互联

    1,在有外网ip的机器上启动server. package udp; import java.net.DatagramPacket; import java.net.InetSocketAddress ...

  8. The AndroidManifest.xml File

    manifest (船运的)载货清单 http://www.android-doc.com/guide/topics/manifest/manifest-intro.html Every applic ...

  9. php网站验证码的生成

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/p ...

  10. 多线程 用户级线程和内核级线程 from C++多核高级编程

    转 http://book.51cto.com/art/201006/206946.htm 6.1.1 用户级线程和内核级线程 2010-06-21 20:37 齐宁/董泽惠 译 清华大学出版社 字号 ...