题解 | #牧场奶牛集合区域#
牧场奶牛集合区域
https://www.nowcoder.com/practice/89218acf98234315af1cb3a223935318
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param groups int整型vector * @param n int整型 * @return int整型vector<vector<>> */ vector<vector<int> > findGatheringAreas(vector<int>& groups, int n) { // write code here vector<vector<int>> result; for (int i = 0;i < groups.size();++i) { vector<int> temp; temp.push_back(groups[i]); for (int j = i + 1;j < groups.size();++j) { if (groups[j] - 1 == groups[i]) { i = j; } else { temp.push_back(groups[i]); break; } if (j == groups.size() - 1) { if (groups[j] - groups[j - 1] == 1) { temp.push_back(groups[j]); } } } if (temp.size() == 1) { temp.push_back(temp[0]); } result.push_back(temp); } return result; } };