You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// vector<int> twoSum(vector<int>& nums, int target) {
54
+
// unordered_map<int, int> hashmap;
55
+
56
+
// for (int i = 0; i < nums.size(); i++) {
57
+
// int complement = target - nums[i];
58
+
59
+
// if (hashmap.find(complement) != hashmap.end()){
60
+
// return {hashmap[complement], i};
61
+
// }
62
+
// hashmap[nums[i]] = i;
63
+
// }
64
+
// return {};
65
+
// }
66
+
// };
67
+
68
+
69
+
70
+
// Visual Explanation
71
+
// Consider the input array nums = [2, 7, 11, 15] and target = 9.
72
+
73
+
// Step-by-step Execution:
74
+
75
+
// Initialization:
76
+
77
+
// An empty hash map (unordered_map) num_map is created.
78
+
// Iteration:
79
+
80
+
// First Iteration (i=0, num=2):
81
+
82
+
// Calculate complement: 9 - 2 = 7.
83
+
// Check if 7 is in num_map: No.
84
+
// Add 2 to num_map with index 0: num_map = {2: 0}.
85
+
// Second Iteration (i=1, num=7):
86
+
87
+
// Calculate complement: 9 - 7 = 2.
88
+
// Check if 2 is in num_map: Yes, it is at index 0.
89
+
// Return indices: [0, 1].
90
+
// Result:
91
+
92
+
// The function returns [0, 1] because nums[0] + nums[1] = 2 + 7 = 9.
93
+
// Code Breakdown
94
+
// Here is the code again for reference:
95
+
96
+
// cpp
97
+
// Copy code
98
+
// #include <iostream>
99
+
// #include <vector>
100
+
// #include <unordered_map>
101
+
102
+
// std::vector<int> twoSum(const std::vector<int>& nums, int target) {
103
+
// std::unordered_map<int, int> num_map;
104
+
105
+
// for (int i = 0; i < nums.size(); ++i) {
106
+
// int complement = target - nums[i];
107
+
108
+
// if (num_map.find(complement) != num_map.end()) {
109
+
// return {num_map[complement], i};
110
+
// }
111
+
112
+
// num_map[nums[i]] = i;
113
+
// }
114
+
115
+
// return {};
116
+
// }
117
+
// Explanation of Syntax and Concepts
118
+
// Function Signature:
119
+
120
+
// cpp
121
+
// Copy code
122
+
// std::vector<int> twoSum(const std::vector<int>& nums, int target)
123
+
// std::vector<int>: The function returns a vector of integers, which are the indices of the two numbers that add up to the target.
124
+
// const std::vector<int>& nums: The function takes a constant reference to a vector of integers as input.
125
+
// const: Ensures that the input vector nums is not modified within the function.
126
+
// &: Denotes that nums is passed by reference, which avoids copying the entire vector and improves performance.
127
+
// Hash Map (unordered_map):
128
+
129
+
// cpp
130
+
// Copy code
131
+
// std::unordered_map<int, int> num_map;
132
+
// This is a hash map that stores each number in nums as a key and its index as the value.
133
+
// Iteration:
134
+
135
+
// cpp
136
+
// Copy code
137
+
// for (int i = 0; i < nums.size(); ++i) {
138
+
// int complement = target - nums[i];
139
+
140
+
// if (num_map.find(complement) != num_map.end()) {
141
+
// return {num_map[complement], i};
142
+
// }
143
+
144
+
// num_map[nums[i]] = i;
145
+
// }
146
+
// We iterate over the nums vector.
147
+
// For each element, calculate the complement (target - nums[i]).
148
+
// Check if the complement exists in num_map.
149
+
// If it does, return the indices of the current element and the complement.
150
+
// If it doesn’t, add the current element and its index to num_map.
151
+
// Why Use const and &?
152
+
// Efficiency: Passing by reference (&) avoids making a copy of the vector, which is especially important for large datasets as it saves memory and processing time.
153
+
// Safety: Using const ensures that the function does not modify the input vector, providing a guarantee about the immutability of nums within the function.
154
+
// Visual Representation
155
+
// Here is a simplified visual representation of how the function processes the array:
156
+
157
+
// plaintext
158
+
// Copy code
159
+
// nums = [2, 7, 11, 15]
160
+
// target = 9
161
+
162
+
// Iteration 1:
163
+
// num = 2, complement = 7
164
+
// num_map: {}
165
+
166
+
// Iteration 2:
167
+
// num = 7, complement = 2
168
+
// num_map: {2: 0}
169
+
// Found complement in num_map: 2 at index 0
170
+
// Return indices: [0, 1]
171
+
// Summary
172
+
// const std::vector<int>& nums: Efficiently passes the input vector by reference without copying and ensures it remains unchanged.
173
+
// Hash Map: Stores elements and their indices for quick lookup of complements.
174
+
// Iteration and Check: Efficiently finds the pair of indices that sum up to the target.
175
+
// Related Suggestions
176
+
// a. Add error handling for edge cases where no solution exists, even though the problem guarantees one.
177
+
178
+
// b. Extend the function to handle multiple solutions and return all pairs that sum up to the target.
0 commit comments