这是我对问题的解决方案:
List<List<int>> consecutive_groups(List<int> a) {
a.sort();
List<List<int>> result = [];
List<int> temp = [];
temp.add(a[0]);
for (int i = 0; i < a.length - 1; i++) {
if (a[i + 1] == a[i] + 1) {
temp.add(a[i + 1]);
} else {
result.add(temp);
temp = [];
temp.add(a[i + 1]);
}
}
result.add(temp);
return result;
}
Run Code Online (Sandbox Code Playgroud)