4Sum_leetCode
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)
N SUM和都是一个德性,代码例如以下:
class Solution
{
private:
vector<vector<int> > ret;
public:
vector<vector<int> > fourSum(vector<int> &num, int target)
{
sort(num.begin(), num.end()); ret.clear(); for(int i = 0; i < num.size(); i++)
{
if (i > 0 && num[i] == num[i-1])
continue; for(int j = i + 1; j < num.size(); j++)
{
if (j > i + 1 && num[j] == num[j-1])
continue; int k = j + 1;
int t = num.size() - 1; while(k < t)
{
if (k > j + 1 && num[k] == num[k-1])
{
k++;
continue;
} if (t < num.size() - 1 && num[t] == num[t+1])
{
t--;
continue;
} int sum = num[i] + num[j] + num[k] + num[t]; if (sum == target)
{
vector<int> a;
a.push_back(num[i]);
a.push_back(num[j]);
a.push_back(num[k]);
a.push_back(num[t]);
ret.push_back(a);
k++;
t--;
}
else if (sum < target)
k++;
else
t--;
}
}
} return ret;
}
};
4Sum_leetCode的更多相关文章
- 详谈Format String(格式化字符串)漏洞
格式化字符串漏洞由于目前编译器的默认禁止敏感格式控制符,而且容易通过代码审计中发现,所以此类漏洞极少出现,一直没有笔者本人的引起重视.最近捣鼓pwn题,遇上了不少,决定好好总结了一下. 格式化字符串漏 ...
随机推荐
- c++ —— .bat 对拍
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...
- [POJ 3378] Crazy Thairs
Link: POJ 3378 传送门 Solution: 按序列长度$dp$, 设$dp[i][j]$为到第$i$个数,符合要求的序列长度为$j$时的序列个数, 易得转移方程:$dp[i][j]=\s ...
- 检索COM 类工厂中CLSID 为{00024500-0000-0000-C000-000000000046}组件时失败
检索 COM 类工厂中 CLSID 为{00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005 当在ASP.NET应用程序中引 ...
- Word插入页码简单方法
适用于只有一个首页,若是从某一页,需要分节符,从某一页参考Word2007插入两种页码. 插入-页码-页面低端-弹到设计窗口.然后选择页码-设置页码格式-起始页码0-勾选首页不同.
- Tiny4412在Ubuntu下给MiniTools添加快捷方式
解压MiniTools-Linux-20140317.tgz root@ubuntu:~/tiny4412/MiniTools-# ls -l total -rw-r--r-- root root M ...
- (转)秒杀系统中如何动态生成下单随机URL
秒杀系统中通常会避免用户之间访问下单页面的URL(避免使用爬虫来造成不公平).所有需要将URL动态化,即使秒杀系统的开发人员也无法在知晓在秒杀开始时的URL.解决办法是在获取秒杀URL的接口中,返回一 ...
- How to create an IPA (Xcode 5)
This tutorial will walk you through the easiest way to generate an IPA using Xcode 5. We will be usi ...
- ubuntu14.04安装 chrome
安装谷歌浏览器,只需要三行代码: 打开终端,输入 cd /tmp 对于谷歌Chrome32位版本,使用如下链接: wget https://dl.google.com/linux/direct/goo ...
- nodejs - 根据用户地址不同 返回不同数据
年前忙疯了 之前写连续上班12天的时候 感觉自己太天真了 年前连续上班20天 真心苦逼成狗 好几次晚上12点到家 然后 最近 也灭有学习太多 就是项目上的 事情 真心忙啊 简单写了一段 Nodej ...
- vector list map 遍历删除指定元素
#include <stdio.h> #include <stdint.h> #include <vector> #include <list> #in ...