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, ...
随机推荐
- 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)
主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...
- Java 新特性(3) - JDK7 新特性
http://www.ibm.com/developerworks/cn/java/j-lo-jdk7-1/ JSR292:支持动态类型语言(InvokeDynamic) 近 年来越来越多的基于 JV ...
- WPF六个控制概述
在线演示:http://v.youku.com/v_show/id_XNzA0NjU1Mjk2.html 清晰版视频+代码下载:http://115.com/lb/5lbcftnrfo9s 一.简单介 ...
- 83. 从视图索引说Notes数据库(上)
索引是数据库系统重要的feature,不管是传统的关系型数据库还是时兴的NoSQL数据库,它攸关查询性能,因而在设计数据库时须要细加考量.然而,Lotus Notes隐藏技术底层.以用户界面为导向.追 ...
- groovy install,gvm,groovysh简述(转)
1.1 安装Groovy Groovy主页:http://www.groovy-lang.org 确保本地系统安装了Java 1.1.1 在Windows系统上安装Groovy 1.创建环境变量GRO ...
- 通过HttpClient来调用Web Api接口,实体参数的传递
下面定义一个复杂类型对象 public class User_Info { public int Id { get; set; } public string Name { get; set; } p ...
- Codeforces Round #306 (Div. 2) C
意甲冠军 到不超过一个更100该整数字符串.采取随机从数间(不能拿). 问:是否有可能被剩下8除尽.假设能,出口YES和任选一个数字的其余病例的. 不能够,输出NO. 思路 想法题. 首先观察到.10 ...
- 运营商网络採用SDN所面临的挑战(一)
运营商网络採用SDN所面临的挑战(一) Babak Samimi 将数据平面.控制平面与管理平面分隔开来所实现的软件定义网络(SDN)改善了OPEX及CAPEX,而且使得网络资源的集中调配和管理成为可 ...
- C#如何设置session过期时间
1.操作系统 步骤:开始——〉管理工具——〉Internet信息服务(IIS)管理器——〉网站——〉默认网站——〉 右键“属性”——〉主目录——〉配置——〉选项——〉启用会话状态——〉会话超时(在 ...
- 从头开始学JavaScript (十一)——Object类型
原文:从头开始学JavaScript (十一)--Object类型 一.object类型 一个object就是一系列属性的集合,一个属性包含一个名字(属性名)和一个值(属性值). object对于在应 ...