56[LeetCode] .Merge Intervals
2024-08-26 07:42:12
Given an array nums
of n integers and an integer target
, are there elements a, b, c, 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;
}
最新文章
- Python 开发轻量级爬虫08
- hihocoder-平衡树&#183;SBT
- mybatis笔记2 基础理论准备
- 关于HTML+CSS设置图片居中的方法
- 支持MVC的代码生成运行效果 C# ASP.NET
- kuangbin_ShortPath J (POJ 1511)
- javascript 对象中的 handleEvent
- 10分钟学会AngularJS的数据绑定
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
- Razor引擎学习:RenderBody,RenderPage和RenderSection
- Spring AOP之异常转换
- node.js第二天之模块
- Javascript高级编程学习笔记(1)—— JS简介
- Hacking /dev/random: Pipe
- Delphi获取本机所有的IP
- CentOS7开放端口号
- Tools:实现vmware虚拟机开机自启动
- python--第十六天总结(bootstrap)
- c#: WebBrowser控制台输出
- 如何在servlet刚启动时候获取服务器根目录?