update project structure
This commit is contained in:
14
leetcode_rs/src/main.rs
Normal file
14
leetcode_rs/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
// src/main.rs
|
||||
|
||||
mod problem_1 {
|
||||
pub mod solution;
|
||||
}
|
||||
|
||||
use problem_1::solution::Solution;
|
||||
|
||||
fn main() {
|
||||
let nums = vec![2, 7, 11, 15];
|
||||
let target = 9;
|
||||
let result = Solution::two_sum(nums, target);
|
||||
println!("{:?}", result); // 输出: [0, 1]
|
||||
}
|
||||
32
leetcode_rs/src/problem_1/solution.rs
Normal file
32
leetcode_rs/src/problem_1/solution.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Solution;
|
||||
|
||||
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().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![]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user