Problem 15 prototype implementation
This commit is contained in:
parent
c56399ab5d
commit
3e15c5bd78
1 changed files with 22 additions and 4 deletions
|
@ -1,8 +1,26 @@
|
|||
fn main() {
|
||||
|
||||
println!("{:?}", three_sum(vec![-1,0,1,2,-1,-4]));
|
||||
}
|
||||
|
||||
pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
|
||||
return vec![];
|
||||
// TODO: fix and optimize
|
||||
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
let mut result: Vec<Vec<i32>> = Vec::new();
|
||||
let mut seen = std::collections::HashSet::<i32>::with_capacity(nums.len());
|
||||
let mut seen2 = std::collections::HashSet::<i32>::with_capacity(nums.len()-1);
|
||||
nums.sort();
|
||||
for i in 0..nums.len()-2 {
|
||||
if seen.contains(&nums[i]) { continue; }
|
||||
for j in i+1..nums.len()-1 {
|
||||
if seen2.contains(&nums[j]) { continue; }
|
||||
for k in j+1..nums.len() {
|
||||
if nums[i] + nums[j] + nums[k] == 0 {
|
||||
result.push(vec![nums[i], nums[j], nums[k]]);
|
||||
}
|
||||
}
|
||||
seen2.insert(nums[j]);
|
||||
}
|
||||
seen.insert(nums[i]);
|
||||
seen2.clear();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue