You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
/
@param{number[][]}intervals
@return{number[][]}/functionmerge(intervals){if(!intervals.length)returnintervals//让每一个数组的第一个数是从小到大排列intervals.sort((a,b)=>a[0]-b[0])//按顺序拿到一个数组letprev=intervals.shift()//res (pass by reference) = prevletres=[prev]// Traverse element in intervals, because we need an array with 2 elements, inseatd of a single number in a specific arrayfor(letcurrofintervals){// must have a =, otherwise cannot pass [[1,4][4,5]]if(curr[0]<=prev[1]){prev[1]=Math.max(prev[1],curr[1])}else{res.push(curr)prev=curr}}returnres}
The text was updated successfully, but these errors were encountered:
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
The text was updated successfully, but these errors were encountered: