Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [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]
]



注意sort() 中的cmp()比较函数的定义要放在类外面:

/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(Interval a,Interval b){return a.start<b.start;}
class Solution {
public: vector<Interval> merge(vector<Interval>& ins) {
if (ins.empty()) return vector<Interval>{};
vector<Interval> res;
sort(ins.begin(), ins.end(), cmp);
res.push_back(ins[]);
for (int i = ; i < ins.size(); i++) {
if (res.back().end < ins[i].start) res.push_back(ins[i]);
else
res.back().end = max(res.back().end, ins[i].end);
}
return res;
}
};

如在在sort中定义排序方法应该这么写:

vector<Interval> merge(vector<Interval>& ins) {
if (ins.empty()) return vector<Interval>{};
vector<Interval> res;
sort(ins.begin(), ins.end(), [](Interval a, Interval b){return a.start < b.start;});
res.push_back(ins[]);
for (int i = ; i < ins.size(); i++) {
if (res.back().end < ins[i].start) res.push_back(ins[i]);
else
res.back().end = max(res.back().end, ins[i].end);
}
return res;
}

最新文章

  1. Python 开发轻量级爬虫08
  2. hihocoder-平衡树&#183;SBT
  3. mybatis笔记2 基础理论准备
  4. 关于HTML+CSS设置图片居中的方法
  5. 支持MVC的代码生成运行效果 C# ASP.NET
  6. kuangbin_ShortPath J (POJ 1511)
  7. javascript 对象中的 handleEvent
  8. 10分钟学会AngularJS的数据绑定
  9. HDU 3746 (KMP求最小循环节) Cyclic Nacklace
  10. Razor引擎学习:RenderBody,RenderPage和RenderSection
  11. Spring AOP之异常转换
  12. node.js第二天之模块
  13. Javascript高级编程学习笔记(1)—— JS简介
  14. Hacking /dev/random: Pipe
  15. Delphi获取本机所有的IP
  16. CentOS7开放端口号
  17. Tools:实现vmware虚拟机开机自启动
  18. python--第十六天总结(bootstrap)
  19. c#: WebBrowser控制台输出
  20. 如何在servlet刚启动时候获取服务器根目录?

热门文章

  1. window下安装composer
  2. 如何在 Mac 上卸载 Java?
  3. 分布式网上商城项目- BeanDefinitionStoreException
  4. Django学习笔记5-url
  5. 在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符。 (#1113)
  6. transform Vs Udf
  7. spark exectors的启动总结
  8. opencv3 学习一 - Visual Studio 配置
  9. 前端css之float浮动
  10. Python学习笔记三:数据类型