package main

import (
	// "fmt"
	. "nc_tools"
	"sort"
)

/*
 * type Interval struct {
 *   Start int
 *   End int
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param intervals Interval类一维数组
 * @return Interval类一维数组
 */
func merge( intervals []*Interval ) []*Interval {
    // write code here
    if len(intervals) == 0 {
        return intervals
    }
    sort.Slice(intervals, func(i, j int) bool {
        if intervals[i].Start < intervals[j].Start {
            return true
        }
        return false
    })
    var res [] *Interval
    tmp := intervals[0]
    for i:=1;i<len(intervals);i++ {
        if intervals[i].Start <= tmp.End && intervals[i].End <= tmp.End{
            continue
        }else if intervals[i].Start <= tmp.End && intervals[i].End >= tmp.End{
            tmp.End = intervals[i].End
        } else {
            res = append(res, tmp)
            tmp = intervals[i]
        }
    }
    res = append(res, tmp)
    return res
}