题解 | #合并区间#

合并区间

https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a

using System;
using System.Collections.Generic;
using System.Linq;
/*
public class Interval
{
	public int start;
	public int end;

	public Interval ()
	{
		start = 0;
		end = 0;
	}

	public Interval (int s, int e)
	{
		start = s;
		end = e;
	}
}
*/

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param intervals Interval类一维数组 
     * @return Interval类一维数组
     */
    public List<Interval> merge (List<Interval> intervals) {
        intervals.Sort((x,y) => x.start.CompareTo(y.start));
		List<Interval> res = new List<Interval>();
		if(intervals.Count != 0) res.Add(intervals[0]);
		for(int i = 1; i < intervals.Count; i++){
			if(res[res.Count - 1].end < intervals[i].start){
				res.Add(intervals[i]);
			}
			else{
				res[res.Count - 1].end = Math.Max(res[res.Count - 1].end, intervals[i].end);

			}
		}
		return res;
    }
}

全部评论

相关推荐

本神尊:看来是没招到小红薯上的人
点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务