• [leetcode]Merge Intervals


    整体不难,一开始以为是线段树,后来仔细看来不需要,从左到右扫,判断是否要merge就是了。此题有几个要注意的地方:1.Java的Comparator要会写;2.循环结束后的ans.add(tmp)不要忘记;3.merge的时候,左右边界要计算一下。

    /**
     * Definition for an interval.
     * public class Interval {
     *     int start;
     *     int end;
     *     Interval() { start = 0; end = 0; }
     *     Interval(int s, int e) { start = s; end = e; }
     * }
     */
    public class Solution {
        public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
            // Start typing your Java solution below
            // DO NOT write main() function              
            int len = intervals.size();
            if (len == 0 || len == 1) return intervals;
            ArrayList<Interval> ans = new ArrayList<Interval>();
            
            Collections.sort(intervals, new IntervalComparator());
            
            Interval tmp = intervals.get(0);
            for (int i = 1; i < len; i++) {
                Interval itv = intervals.get(i);
                if (tmp.end >= itv.start) { // mergeable
                    int left = Math.min(tmp.start, itv.start);
                    int right = Math.max(tmp.end, itv.end);
                    tmp = new Interval(left, right);
                }
                else {
                    ans.add(tmp);
                    tmp = intervals.get(i);
                }
            }
            ans.add(tmp);
            return ans;
        }
    }
    
    class IntervalComparator implements Comparator<Interval>
    {
        public int compare(Interval a, Interval b) {
            return a.start - b.start;
        }
    }
    

    第二刷:

    bool cmp(const Interval &a, const Interval &b)
    {
        if (a.start != b.start)
            return a.start < b.start;
        else
            return a.end < b.end;
    }
    
    class Solution {
    public:
        vector<Interval> merge(vector<Interval> &intervals) {
            vector<Interval> result;
            sort(intervals.begin(),intervals.end(), cmp);
            for (int i = 0; i < intervals.size(); i++)
            {
                if (result.size() == 0 || result.back().end < intervals[i].start)
                {
                    result.push_back(intervals[i]);
                }
                else if (intervals[i].end > result.back().end)
                {
                    result.back().end = intervals[i].end;
                }
            }
            return result;
        }
    };
    

    python3,另一种做法是排序各个点

    class Solution:
        def merge(self, intervals: List[List[int]]) -> List[List[int]]:
            points = []
            for interval in intervals:
                points.append((interval[0], -1))
                points.append((interval[1], 1))
                
            points = sorted(points)
            
            result = []
            cnt = 0
            begin = 0
            end = 0
            for point in points:
                cnt += point[1]
                if cnt == -1 and point[1] == -1: # begin
                    begin = point[0]
                if cnt == 0 and point[1] == 1: # end
                    end = point[0]
                    result.append([begin, end])
                    
            return result
    

      

  • 相关阅读:
    Spring事务的一些基本知识(一)
    Redis管道
    Spring事务的一些基本知识(四)大事务的危害与优化
    登录页面测试点
    朋友圈点赞用例的设计点
    面向对象
    函数的重载
    构造代码块和静态代码块,构造函数的执行
    单例设计模式
    三分查找(2020icp南京F)
  • 原文地址:https://www.cnblogs.com/lautsie/p/3254191.html
Copyright © 2020-2023  润新知