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

Loading…
Cancel
Save