[ 命名建議 ] LeetCode 56. Merge Intervals #129
Answered
by
twy30
LPenny-github
asked this question in
Q&A
-
|
LeetCode: 線上教材: 再麻煩 @twy30 給予命名建議 orz 感激不盡 using System;
using System.Collections.Generic;
public class MergeIntervals
{
public int[][] Merge(int[][] intervals)
{
if (intervals.Length <= 1) { return intervals; }
Array.Sort(intervals, (a, b)=>a[0].CompareTo(b[0]));
Stack<int[]> mergedIntervals = new Stack<int[]>();
for (int i = 0; i < intervals.Length; ++i)
{
int[] temp = new []{intervals[i][0],intervals[i][1]};
if (mergedIntervals.Count != 0 && temp[1] <= mergedIntervals.Peek()[1]){continue;}
while (mergedIntervals.Count != 0 && temp[0] <= mergedIntervals.Peek()[1])
{
temp = new []{mergedIntervals.Peek()[0],
Math.Max(temp[1],mergedIntervals.Pop()[1])};
}
mergedIntervals.Push(temp);
}
int[][] output = new int[mergedIntervals.Count][];
for (int k = mergedIntervals.Count -1; k >= 0; --k)
{
output[k] = new []{mergedIntervals.Peek()[0], mergedIntervals.Pop()[1]};
}
return output;
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
twy30
Feb 25, 2021
Replies: 1 comment 2 replies
-
|
你好 😊 我覺得每個變數都命名的不錯。 👌 |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
LPenny-github
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@LPenny-github
你好 😊
我覺得每個變數都命名的不錯。 👌