Skip to content

Instantly share code, notes, and snippets.

@tangyumeng
Created January 24, 2020 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangyumeng/70810826ac3efa8829ed514f813fb83a to your computer and use it in GitHub Desktop.
Save tangyumeng/70810826ac3efa8829ed514f813fb83a to your computer and use it in GitHub Desktop.
已排序数组,以相同元素为组拆分数组
extension Array {
func split(where condation: (Element, Element) -> Bool) -> [[Element]] {
var result: [[Element]] = self.isEmpty ? [] : [[self[0]]]
for (previous, current) in zip(self, self.dropFirst()) {
if (condation(previous, current)) {
result.append([current])
} else {
result[result.endIndex - 1].append(current)
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment