rewrite 1 in rust
This commit is contained in:
parent
e25ed492f3
commit
6866cf0b82
30
leetcode/1.rs
Normal file
30
leetcode/1.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
/// This function takes a vector of integers (`nums`) and an integer (`target`) as input.
|
||||
/// It returns a vector of integers.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `nums` - A vector of integers.
|
||||
/// * `target` - An integer that represents the target sum.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// This function returns a vector of integers.
|
||||
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for (i, &num) in nums.iter_mut().enumerate() {
|
||||
let complement = target - num;
|
||||
|
||||
if let Some(&index) = map.get(&complement) {
|
||||
return vec![index as i32, i as i32];
|
||||
}
|
||||
|
||||
map.insert(num, i);
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user