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

g0001_0100.s0001_two_sum.Solution.cpp 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_05_12_Time_4_ms_(94.42%)_Space_14.1_MB_(17.14%)

#include 
#include 

class Solution {
public:
    std::vector twoSum(std::vector& numbers, int target) {
        std::unordered_map indexMap;
        for (int i = 0; i < numbers.size(); i++) {
            int requiredNum = target - numbers[i];
            if (indexMap.find(requiredNum) != indexMap.end()) {
                return {indexMap[requiredNum], i};
            }
            indexMap[numbers[i]] = i;
        }
        return {-1, -1};
    }
};




© 2015 - 2025 Weber Informatics LLC | Privacy Policy