Skip to content

Commit

Permalink
Merge pull request youngyangyang04#808 from Jerry-306/patch-25
Browse files Browse the repository at this point in the history
新增 0056 合并区间 JavaScript 另一种解法
  • Loading branch information
youngyangyang04 authored Oct 4, 2021
2 parents 3a98b94 + ef2eb4b commit 87f9a26
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions problems/0056.合并区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```



Expand Down

0 comments on commit 87f9a26

Please sign in to comment.