LeetCode_ 4 sum
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)
无聊 3sum 的变形
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int>> res;
int len = num.size();
if(len < ) return res;
sort(num.begin(),num.end());
for(int i = ; i< len-;++i){
while(i> && i< len- && num[i] == num[i-])++i;
for(int j = i+; j< len-; ++j){
while(j!=i+ && j< len-&&num[j] == num[j-])++j;
int left = j+;
int right = len-;
while(left < right){
int sum = num[i] + num[j] +num[left]+num[right];
if(sum == target){
vector<int> ans;
ans.push_back(num[i]);
ans.push_back(num[j]);
ans.push_back(num[left]);
ans.push_back(num[right]);
res.push_back(ans);
left++;
while(left<right && num[left]==num[left-]) ++left;
right--;
while(left < right && num[right] == num[right+]) --right;
}else if(sum <target){
left++;
while(left<right && num[left]==num[left-]) ++left;
}else{
right--;
while(left < right && num[right] == num[right+]) --right;
}
}//while(left <right)
}//for j
}// for i
return res;
}
};
LeetCode_ 4 sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- 从 Kubernetes 谈容器网络
基本概念 在 Kubernetes 中.资源从管理粒度上分为三级:容器.Pod.Service. 容器 即 Docker 或者 Rocket 容器(1.0 中仅支持这两种容器). 容器是最低粒度的资源 ...
- 关于这两天研究Java打印pdf方法的记录
这两天在研究Java调用打印机打印PDF文件的方法,学到了不少东西,特别来记录一下. 关于Java打印网上最多的而且也是Java正统的打印方法就是使用PrintService,一套比較标准的打印代码例 ...
- phoneGap 中修改生成APP的名字
最近忙着研究移动开发的事情,去学习了一下移动开发的东西,例如eclipse和phoneGap进行配合使用,感觉还是不错的,先针对eclipse和phoneGa的平台搭建这里先不在详细说啦,主要还是我们 ...
- 2013ACM暑假集训总结-致将走上大三征途的我
回想起这个暑假,从开始与雄鹰一起的纠结要不要进集训队,与吉吉博博组队参加地大邀请赛,害怕进不了集训队.当时激励我月份开始接触的,记得当时在弄运动会来着,然后就问了雄鹰一些输入输出的东西,怀着满心的期待 ...
- [Javascript] Automating Releases with semantic-release
There are so many repeated steps when releasing a new version of a library. The tool semantic-releas ...
- Go语言学习笔记(一) : 搭建Windows下的Go开发环境
最近突然对Go语言产生了兴趣,主要是因为在使用python的时候遇到了一些不爽的问题,然后发现了Go.Go是Google出的一个动态语言,语法和C++接近,性能也非常的好,而且还支持编译成exe发布, ...
- Core Foundation框架介绍
Core Foundation框架介绍 **参考网址: ARC下OC对象和CF对象之间的桥接 Core Foundation框架介绍 Core Foundation框架 Core Foundation ...
- u盘安装linux(windows7+linux双系统)
前提条件:1.先装windows7,后装linux系统 2.windows7 里有“未分区的空间”(不是C:,D:,E:) :计算机→管理→存储空间,删除一些压缩卷即可. 3.下载ultraiso → ...
- Android常用功能代码块(转)
1.设置activity无标题,全屏 // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow(). ...
- TCP和SSL
查看TCP和SSL的握手时间: curl -w "TCP handshake: %{time_connect}, SSL handshake: %{time_appconnect}\n&qu ...