• 56[LeetCode] .Merge Intervals



    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[0]); for (int i = 1; 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[0]);
        for (int i = 1; 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;
    }
  • 相关阅读:
    中国各省份绘制SVG地图数据
    cookie sessionStorage localStorage 区别
    CSS隐藏元素的几种方法
    15款增强web体验的Javascript库
    HTTP状态码
    IE CSS HACK
    网站性能优化(Yahoo 35条)
    几款超实用的 CSS 开发工具
    Linux 日志切割工具cronolog详解
    linux 文件搜索命令
  • 原文地址:https://www.cnblogs.com/250101249-sxy/p/10422535.html
Copyright © 2020-2023  润新知