LeetCode Solutions C++ All Boxed
LeetCode Solutions C++ All Boxed
1. Two Sum
Description:
Given an array of integers nums and an integer target, return indices of the two numbers such that
they add up to target. You may assume that each input would have exactly one solution, and you
Input/Output:
Output: [0,1]
Explanation:
Approach:
- Use a hash map to store the complement (target - nums[i]) and index.
- As we iterate through the array, check if the current number exists in the hash map.
C++ Code:
#include <vector>
#include <unordered_map>
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
if (hashmap.find(complement) != hashmap.end()) {
hashmap[nums[i]] = i;
return {};
};