update project structure

This commit is contained in:
2024-06-20 06:29:07 +08:00
parent 17b4ef8970
commit fdaa94a0fa
49 changed files with 35 additions and 4 deletions

14
leetcode_rs/src/main.rs Normal file
View 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]
}

View 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![]
}
}