题解 | #牧场奶牛集合区域#

牧场奶牛集合区域

https://www.nowcoder.com/practice/89218acf98234315af1cb3a223935318

package main


/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param groups int整型一维数组 
 * @param n int整型 
 * @return int整型二维数组
*/
func findGatheringAreas( groups []int ,  n int ) [][]int {
    // write code here
    if n==0{
        return [][]int{}
    }
    ans :=make([][]int,0)
    left,right :=groups[0],groups[0]
    for i :=1;i<len(groups);i++{
        if groups[i]!=groups[i-1]+1{
            ans = append(ans, []int{left,right})
            left,right =groups[i],groups[i]
        }
        right=groups[i]
        if i==len(groups)-1{
            ans = append(ans, []int{left,right})
        }
        
    }
    return ans
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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