first commit
This commit is contained in:
18
src/main.rs
18
src/main.rs
@@ -3,7 +3,21 @@
|
||||
* Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
|
||||
*-------------------------------------------------------------------------------------------------------------*/
|
||||
|
||||
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
|
||||
use std::collections::HashMap;
|
||||
let mut map = HashMap::new();
|
||||
for (i, &num) in nums.iter().enumerate() {
|
||||
if let Some(&j) = map.get(&(target - num)) {
|
||||
return vec![j as i32, i as i32];
|
||||
}
|
||||
map.insert(num, i);
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let name = "VS Code Remote - Containers";
|
||||
println!("Hello, {}!", name);
|
||||
let nums = vec![2, 7, 11, 15];
|
||||
let target = 9;
|
||||
let result = two_sum(nums, target);
|
||||
println!("{:?}", result); // 输出: [0, 1]
|
||||
}
|
||||
Reference in New Issue
Block a user