-
-
Notifications
You must be signed in to change notification settings - Fork 101
Add: Two Sum (C++) #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add: Two Sum (C++) #187
Conversation
Reviewer's GuideIntroduces a linear-time C++ implementation for the Two Sum problem by leveraging an unordered_map to track previously seen numbers and their indices for constant-time complement lookups. Class diagram for the new Solution class (Two Sum)classDiagram
class Solution {
+std::vector<int> twoSum(std::vector<int>& nums, int target)
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Consider using auto it = numMap.find(complement) and checking it != end() to avoid two hash lookups (count + operator[]).
- Reserve space in numMap (numMap.reserve(nums.size())) before the loop to reduce rehashing overhead for large inputs.
- Use size_t for the loop index and n (instead of int) to match nums.size() return type and avoid type mismatches.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using auto it = numMap.find(complement) and checking it != end() to avoid two hash lookups (count + operator[]).
- Reserve space in numMap (numMap.reserve(nums.size())) before the loop to reduce rehashing overhead for large inputs.
- Use size_t for the loop index and n (instead of int) to match nums.size() return type and avoid type mismatches.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
@rishanmenezes raise an ISSUE as well,,, as it;'s not present at the existing ISSUE list,,, |
i have raised an issue as well |
|
PR Title Format: 1. Two Sum.cpp
Intuition
The "Two Sum" problem asks us to find two numbers in an array that add up to a specific target. A brute-force approach would involve checking every possible pair, which is O(n^2). To optimize this, we can use a hash map (unordered_map in C++) to store numbers we've already seen along with their indices. This allows us to quickly check if the complement (target - current number) exists in the map.
Approach
Initialize an unordered_map called numMap to store numbers and their indices.
Iterate through the input array nums with an index i .
For each number nums[i] , calculate the complement needed to reach the target (i.e., target - nums[i] ).
Check if the complement exists as a key in numMap .
If the complement is not found, add the current number nums[i] and its index i to the numMap .
If the loop completes without finding a pair (which shouldn't happen based on the problem constraints, as a solution is guaranteed), return an empty vector or handle as appropriate.Code Solution (C++)
Related Issues
#193
By submitting this PR, I confirm that:
Summary by Sourcery
New Features: