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
56. 合并区间
给出一个区间的集合,请合并所有重叠的区间。
示例 1:
输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入: [[1,4],[4,5]] 输出: [[1,5]] 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
The text was updated successfully, but these errors were encountered:
解题思路:
/** * @param {number[][]} intervals * @return {number[][]} */ var merge = function (intervals) { if (intervals.length < 2) { return intervals; } intervals.sort((a, b) => a[0] - b[0]); let currArr = intervals[0], result = []; for (const interval of intervals) { if (currArr[1] >= interval[0]) { currArr[1] = Math.max(currArr[1], interval[1]); } else { result.push(currArr); currArr = interval; } } if (currArr.length > 0) { result.push(currArr); } return result; };
Sorry, something went wrong.
/** * @param {number[][]} intervals * @return {number[][]} */ var merge = function(intervals) { if (!intervals.length) return []; intervals.sort((a, b) => a[0] - b[0]); let res = [intervals[0]]; for (let i = 1; i < intervals.length; i++) { if (intervals[i][0] > res[res.length - 1][1]) { res.push(intervals[i]); } else if (intervals[i][1] > res[res.length - 1][1]) { res[res.length - 1][1] = intervals[i][1]; } } return res; };
No branches or pull requests
56. 合并区间
给出一个区间的集合,请合并所有重叠的区间。
示例 1:
示例 2:
The text was updated successfully, but these errors were encountered: