leetcode第17题--4Sum
Problem: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)
这题其实就是之前的变种,我是这样想的,首先还是排序好,然后根据固定四个的头和尾,即i为头,从0开始到倒数第四个,j为尾巴从尾开始到i+2. 再来一个left=i+1 right=j-1 如果i++之后和前面一个相等,那就直接continue,因为已经在之前一个i算过了,同理,j--之后如果和之前的j相等的话也是不用计算的直接continue 这样的话复杂度就是n3方
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target)
{
vector<vector<int> > sum;
sum.clear();
if(num.size() < )
return sum;
sort(num.begin(), num.end());
for (int i = ; i < num.size() - ; ++i)
{
if (i - >= && num[i - ] == num[i])
continue;
for (int j = num.size() - ; j > i + ; --j)
{
if(j + < num.size() && num[j + ] == num[j])
continue;
int left = i + , right = j - ;
while(left < right)
{
if (num[i] + num[left] + num[right] + num[j] == target)
{
if(sum.size()== || sum.size()> && !(sum[sum.size() - ][]==num[i] && sum[sum.size() - ][]==num[left] && sum[sum.size() - ][]==num[right]))
{
vector<int> tep;
tep.push_back(num[i]);
tep.push_back(num[left]);
tep.push_back(num[right]);
tep.push_back(num[j]);
sum.push_back(tep);
}
left++;
right--;
}
else if (num[i] + num[left] + num[right] + num[j] < target)
left++;
else if (num[i] + num[left] + num[right] + num[j] > target)
right--;
}
}
}
return sum;
}
};
leetcode第17题--4Sum的更多相关文章
- LeetCode第[17]题(Java):Letter Combinations of a Phone Number
题目:最长公共前缀 难度:EASY 题目内容: Given a string containing digits from 2-9 inclusive, return all possible let ...
- LeetCode 第17题--电话号码的组合(DFS)
1. 题目 2.题目分析与思路 3.代码 1. 题目 输入:"23" 输出:["ad", "ae", "af", &qu ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- Leetcode第1题:两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素.示例:给定 nums ...
- 【JavaScript】Leetcode每日一题-青蛙过河
[JavaScript]Leetcode每日一题-青蛙过河 [题目描述] 一只青蛙想要过河. 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有). 青蛙可以跳上石子 ...
- 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素
[python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...
- 【JavaScript】【dp】Leetcode每日一题-解码方法
[JavaScript]Leetcode每日一题-解码方法 [题目描述] 一条包含字母 A-Z 的消息通过以下映射进行了 编码 : 'A' -> 1 'B' -> 2 ... 'Z' -& ...
- [LeetCode每日一题]153.寻找旋转排序数组中的最小值
[LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...
随机推荐
- slf自己主动绑定实现类过程推断
依照绑定实现类的方式是基于约定原则:推断分下面几个步骤 1.LoggerFactory扫描实现类路径有几个实现类,即在org/slf4j/impl/下有几个StaticLoggerBinder.cla ...
- arcgis,mapinfo(mapxtreme),openlayers专业GIS系统开发
专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP, ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- iptables惹的祸
我的工作环境总是一台Mac机+在Mac机上跑的虚拟机(Virtual Box) 有一个错误我总是在犯:宿主机总是无訪问到虚拟机里的web服务. 解决方法也非常easy:那就是关闭虚拟机上的防火墙!!! ...
- Codeforces 437A The Child and Homework
题目链接:Codeforces 437A The Child and Homework 少看了一个条件,最后被HACK掉到203名,要不然就冲到100多一点了==.. 做这个题收获最大的是英语,A t ...
- cocos2d 消除类游戏简单的算法 (一)
1. 游戏视频演示 2.三消游戏我的理解 上面视频中的游戏.我做了2个星期时间,仅仅能算个简单Demo,还有bug.特效也差点儿没有.感觉三消游戏主要靠磨.越磨越精品. 市场上三消游戏已经超级多了.主 ...
- Eclipse正在使用Ant扑灭Android数据包错误的解决方案 – Perhaps JAVA_HOME does not point to the JDK
问题描写叙述: 在Eclipse中执行ant批量打包工具出错,日志信息例如以下: D:\Android\android-sdk-windows\tools\ant\build.xml:601: The ...
- NPOI以及在ASP.NET MVC中的使用
NPOI以及在ASP.NET MVC中的使用 1.前言 相信大家在工作中经常要遇到一些导入导出Execl操作.学习贵在分享,分享使人快乐,园子里的前辈已经有很多好的文章,鄙人也是能力有限,在这里把这些 ...
- JavaScript中的各种奇葩问题
原文:JavaScript中的各种奇葩问题 JavaScript浮点数 var a = (0.1 + 0.2) + 0.3; var b = 0.1 + (0.2 + 0.3); console.lo ...
- tomcat如何避免遭遇ClassNotFoundException
于Tomcat紧接着为什么要创建一个类加载器Thread.currentThread().setContextClassLoader(catalinaLoader)?这里加载失败主要是为了避免以后加载 ...