题解 | #合并区间#

合并区间

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;
    }
}

全部评论

相关推荐

02-21 23:34
已编辑
厦门大学 Java
神哥不得了:神哥来啦~首先你的bg的话应该算是很好的了,可以把其他删掉,不需要手搓项目呀,直接找网上的项目看懂就行,第一个项目的话虽然和JAVA没有关系,但是他的星数很多,说明你的编程能力还是很强的,我觉得第一个项目是可以放上去的,但是第二个项目的话建议还是再换一个高质量的项目,感觉如果你再把高频top 50的八股再巩固几遍,完全有机会在没有实习的情况下,从暑期实习的大厂,机会还是很大的,注意别看一些假高频八股就行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务