题解 | #牧场奶牛集合区域#
牧场奶牛集合区域
https://www.nowcoder.com/practice/89218acf98234315af1cb3a223935318
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param groups int整型一维数组 * @param n int整型 * @return int整型二维数组 */ function findGatheringAreas(groups, n) { // write code here let res = [] groups.sort((a, b) => a - b) let container = [] for (let i = 0; i < groups.length; i++) { container.push(groups[i]) if (groups[i] + 1 !== groups[i + 1]) { if (container.length === 1) { res.push([container[0], container[0]]) } else { res.push([container[0], container[container.length - 1]]) } container = [] } } return res } module.exports = { findGatheringAreas: findGatheringAreas };