All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0001_two_sum.Solution.rs Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
// #2024_08_24_Time_0_ms_(100.00%)_Space_2.4_MB_(23.16%)

use std::collections::HashMap;

impl Solution {
    pub fn two_sum(numbers: Vec, target: i32) -> Vec {
        let mut index_map: HashMap = HashMap::new();
        
        for (i, &num) in numbers.iter().enumerate() {
            let required_num = target - num;
            if let Some(&index) = index_map.get(&required_num) {
                return vec![index as i32, i as i32];
            }
            index_map.insert(num, i);
        }
        
        vec![-1, -1]
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy