mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
rs: add «442. Find All Duplicates in an Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a56a328d6b
commit
48d437c878
1 changed files with 17 additions and 0 deletions
17
rs/find-all-duplicates-in-an-array.rs
Normal file
17
rs/find-all-duplicates-in-an-array.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
impl Solution {
|
||||
pub fn find_duplicates(mut nums: Vec<i32>) -> Vec<i32> {
|
||||
let mut duplicates = vec![];
|
||||
|
||||
for i in 0..nums.len() {
|
||||
let index = nums[i].abs() - 1;
|
||||
|
||||
if nums[index as usize] < 0 {
|
||||
duplicates.push(index + 1);
|
||||
} else {
|
||||
nums[index as usize] *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
duplicates
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue