1
Fork 0
leetcode/source/two_sum/mod.rs

18 lines
465 B
Rust

use std::convert::TryInto;
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
for (index_1, number_1) in numbers.iter().enumerate() {
for (index_2, number_2) in numbers.iter().enumerate() {
if index_1 == index_2 {
continue;
}
if number_1 + number_2 == target {
return vec![index_1.try_into().unwrap(), index_2.try_into().unwrap()];
}
}
}
panic!("No pair of numbers found that sum to {}", target)
}