
g0001_0100.s0001_two_sum.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #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