diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 93c0a2a040..fd914497ad 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -241,6 +241,32 @@ var merge = function (intervals) { return result }; ``` +版本二:左右区间 +```javascript +/** + * @param {number[][]} intervals + * @return {number[][]} + */ +var merge = function(intervals) { + let n = intervals.length; + if ( n < 2) return intervals; + intervals.sort((a, b) => a[0]- b[0]); + let res = [], + left = intervals[0][0], + right = intervals[0][1]; + for (let i = 1; i < n; i++) { + if (intervals[i][0] > right) { + res.push([left, right]); + left = intervals[i][0]; + right = intervals[i][1]; + } else { + right = Math.max(intervals[i][1], right); + } + } + res.push([left, right]); + return res; +}; +```