improve on repeated groups implementation

main
William Perron 2 years ago
parent 087137a74a
commit 8f18df7d88
No known key found for this signature in database
GPG Key ID: D1815C43C9BA3DE1

@ -16,32 +16,20 @@ fn main() {
fn repeated_groups(nums: Vec<isize>) -> Vec<Vec<isize>> {
let mut i = 0;
let mut j = 0;
let mut j = i + 1;
let mut res: Vec<Vec<isize>> = vec![];
let mut curr: Vec<isize> = vec![];
while j < nums.len() {
if nums[i] != nums[j] {
if curr.len() > 1 {
res.push(curr);
}
i = j;
while j < nums.len() && nums[i] == nums[j] {
j += 1;
curr = vec![nums[i]];
continue;
}
curr.push(nums[i]);
j += 1;
if j - i > 1 {
res.push(nums[i..j].to_vec());
}
// If the last iteration is a repeated group, the loop exits before appending
// the last group to the results, so we check here for what the last group was
// and add it to the result if need be.
if curr.len() > 1 {
res.push(curr);
i = j;
j += 1;
}
res

Loading…
Cancel
Save