We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 数组, 排序
以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。
intervals
示例 1:
输入:intervals = [[1,3],[2,6],[8,10],[15,18]] 输出:[[1,6],[8,10],[15,18]] 解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入:intervals = [[1,4],[4,5]] 输出:[[1,5]] 解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。
提示:
intervals[i].length == 2
Language: JavaScript
/** * @param {number[][]} intervals * @return {number[][]} */ var merge = function (intervals) { let res = [] intervals.sort((a, b) => a[0] - b[0]) let prev = intervals[0] for (let i = 1; i < intervals.length; i++) { let cur = intervals[i] if (prev[1] >= cur[0]) { prev[1] = Math.max(prev[1], cur[1]) } else { res.push(prev) prev = cur } } res.push(prev) return res }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
56. 合并区间
Description
Difficulty: 中等
Related Topics: 数组, 排序
以数组
intervals
表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。示例 1:
示例 2:
提示:
intervals[i].length == 2
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: