题解 | #合并区间#

合并区间

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

/*
 * function Interval(a, b){
 *   this.start = a || 0;
 *   this.end = b || 0;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param intervals Interval类一维数组 
 * @return Interval类一维数组
 */
function merge( intervals ) {
    // write code here
    if(!intervals.length) return []
    let i=1;
    intervals = intervals.sort((i,c)=>i.start-c.start)
    const res = [intervals[0]]
    while(i<intervals.length){
        // console.log(res)
        if(res[res.length-1].end >= intervals[i].start){
            // console.log("--",intervals[i])
            res[res.length-1].end = Math.max(intervals[i].end,res[res.length-1].end);
            i++
            // console.log(JSON.stringify(res))
            continue;
        }
        res.push(intervals[i])
        i++
    

    }
    return res

}
module.exports = {
    merge : merge
};

全部评论

相关推荐

05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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