题解 | #牧场奶牛集合区域#
牧场奶牛集合区域
https://www.nowcoder.com/practice/89218acf98234315af1cb3a223935318
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param groups int整型一维数组 # @param n int整型 # @return int整型二维数组 # class Solution: def findGatheringAreas(self , groups: List[int], n: int) -> List[List[int]]: # write code here res = [] start, end = 0, 1 n = len(groups) if n == 0: return [[0]] while end<n: if res == []: res.append([groups[start], groups[start]]) elif groups[end]-res[-1][1] == 1: res[-1][1] = groups[end] end += 1 else: start = end res.append([groups[start], groups[start]]) end += 1 return res