【leetcode】4Sum
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)
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的更多相关文章
- 【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 ...
- 【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 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- java连接sql server2000/2005
接触Java或者JSP,难免会使用到数据库SQL Server 2000/2005(我使用2005标准版[9.0.3054]测试),经过自己的搜索和研究,使用JDBC连接SQL Server成功,特此 ...
- .NET Core 在Visual Studio 2015 下的使用-MSDN
.NET Core RC2 现已推出,这是真正的"候选发布"而非 RC1 Beta 冒充的候选发布(如果是那样,请考虑发布后出现的所有更改).当前,围绕 .NET Core 的开发 ...
- form表单提交和ajax提交的区别
form表单是整个页面跳到服务器的地址然后提交数据: ajax是往这个地址post数据 <form style="padding:0px;margin:0px;" targe ...
- C语言+SDL2 图形化编程
程设大作业小火车第一版本是命令行界面,第二版本是图形化界面,由于egg库对以后工程开发没有用,我不想用egg库,花了很长时间浏览了一下OpenGL的中文教程,觉得好复杂,需要看很多很多才能写出个简单的 ...
- 决策树笔记:使用ID3算法
决策树笔记:使用ID3算法 决策树笔记:使用ID3算法 机器学习 先说一个偶然的想法:同样的一堆节点构成的二叉树,平衡树和非平衡树的区别,可以认为是"是否按照重要度逐渐降低"的顺序 ...
- codevs3031 最富有的人
题目描述 Description 在你的面前有n堆金子,你只能取走其中的两堆,且总价值为这两堆金子的xor值,你想成为最富有的人,你就要有所选择. 输入描述 Input Description 第一行 ...
- 界面原型Axure
页面原型工具 Axure 超实用页面原型工具.好的页面原型是项目组成员顺利沟通的一个非常重要因素,Axure能快速制作页面原型,还能界面手动式加上事件,链接跳转,弹出层等等一切HTML开发中常用功能, ...
- javascript判断文件大小
<input type="file" id="fileName" name ="fileName" onchange="Ge ...
- js中改变不同的div属性值的操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux学习之用户管理
用户管理是在root用户下进行相关操作的 1.配置文件路径: 保存用户信息的文件:/etc/passwd 保存密码的文件:/etc/shadow 保存用 ...