首页 > 其他分享 >[LeetCode]Merge Intervals

[LeetCode]Merge Intervals

时间:2023-02-02 15:37:14浏览次数:33  
标签:Intervals Interval List start high Merge intervals ans LeetCode


Question


本题难度Hard。

排序+双指针

【复杂度】
时间 O(Nlog(N)) 空间 O(N)

【思路】
先按照每个元素的​​​start​​​从小到大进行排序。然后利用双指针法,设置区间​​(low,high)​​​,如果新的元素​​i​​​的​​start​​​要大于​​high​​​,说明​​(low,high)​​​没有overlap,就将​​(low,high)​​放入结果中,然后将双指针移动:

low=i.start;
high=i.end;

否则就进行合并。

【注意】

1、第30行别忘写了,循环中并没有让最后一个间隔加入到结果中

2、判断重叠的边界时包含等于的情况

3、在sort方法中,我本来想写成:

private void sort(List<Interval> intervals) {

不过这样做比较麻烦,因为是sort in place。所以我改为:

private List<Interval> sort(List<Interval> intervals) {

返回的是重新生成的​​List<Interval>​​对象

【附】
​[Leetcode] Merge Intervals and Insert Interval 合并间隔与插入间隔​​对Collection的排序方法要更为简单方便:

// 按照起点排序
Collections.sort(intervals, new Comparator<Interval>(){
public int compare(Interval i1, Interval i2){
return i1.start - i2.start;
}
});

【代码】

/**
* 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 List<Interval> merge(List<Interval> intervals) {
//require
List<Interval> ans=new LinkedList<Interval>();
if(intervals==null)
return ans;
if(intervals.size()<1)
return ans;
//sort
List<Interval> sorted=sort(intervals);
//invariant
int low=sorted.get(0).start,high=sorted.get(0).end;
for(Interval i:sorted){
if(high<i.start){
ans.add(new Interval(low,high));
low=i.start;high=i.end;
}else{
high=Math.max(high,i.end);
}
}
ans.add(new Interval(low,high));
//ensure
return ans;
}

private List<Interval> sort(List<Interval> intervals) {
int size=intervals.size();
//base case
if(size<2)
return intervals;

Interval mid=intervals.get(size/2);
int pivot=mid.start;
List<Interval> lefts=new LinkedList<Interval>(),rights=new LinkedList<Interval>(),ans=new LinkedList<Interval>();
for(Interval i:intervals){
if(i.start<pivot)
lefts.add(i);
else if(i.start>pivot)
rights.add(i);
else
mid.end=Math.max(i.end,mid.end);
}
for(Interval i:sort(lefts)){
ans.add(i);
}
ans.add(mid);
for(Interval i:sort(rights)){
ans.add(i);
}
return ans;
}
}


标签:Intervals,Interval,List,start,high,Merge,intervals,ans,LeetCode
From: https://blog.51cto.com/u_9208248/6033689

相关文章

  • [LeetCode]Maximum Subarray
    QuestionFindthecontiguoussubarraywithinanarray(containingatleastonenumber)whichhasthelargestsum.Forexample,giventhearray[-2,1,-3,4,-1,2,1......
  • [LeetCode]Spiral Matrix
    QuestionGivenamatrixofmxnelements(mrows,ncolumns),returnallelementsofthematrixinspiralorder.Forexample,Giventhefollowingmatrix:[[1......
  • [LeetCode]N-Queens II
    QuestionFollowupforN-Queensproblem.Now,insteadoutputtingboardconfigurations,returnthetotalnumberofdistinctsolutions.本题难度Hard。【思路】​N......
  • [LeetCode]Group Anagrams
    QuestionGivenanarrayofstrings,groupanagramstogether.Forexample,given:[“eat”,“tea”,“tan”,“ate”,“nat”,“bat”],Return:[[“ate”,“......
  • [LeetCode]Jump Game II
    QuestionGivenanarrayofnon-negativeintegers,youareinitiallypositionedatthefirstindexofthearray.Eachelementinthearrayrepresentsyourmaximu......
  • [LeetCode]Rotate Image
    QuestionYouaregivenannxn2Dmatrixrepresentinganimage.Rotatetheimageby90degrees(clockwise).Followup:Couldyoudothisin-place?本题难度Mediu......
  • LeetCode 1143_ 最长公共子序列
    LeetCode1143:最长公共子序列题目给定两个字符串text1和text2,返回这两个字符串的最长公共子序列的长度。如果不存在公共子序列,返回0。一个字符串的子序列......
  • 【DFS】LeetCode 124. 二叉树中的最大路径和
    题目链接124.二叉树中的最大路径和思路一个子树内部的最大路径和=左子树提供的最大路径和+根节点值+右子树提供的最大路径和。即dfs(root.left)+root.val+dfs(r......
  • LeetCode - 709. To Lower Case
    题目ImplementfunctionToLowerCase()thathasastringparameterstr,andreturnsthesamestringinlowercase.Example1:Input:"Hello"Output:"hello"Example2:......
  • LeetCode - 344. Reverse String
    题目Writeafunctionthatreversesastring.Theinputstringisgivenasanarrayofcharacterschar[].Donotallocateextraspaceforanotherarray,youmust......