Skip to content
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

Solved Two-Sum Problem from LeetCode #13

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

Dhavisco
Copy link
Contributor

@Dhavisco Dhavisco commented Oct 2, 2023

Solution - Two Sum Problem using C++ from LeetCode

This solution is implemented in C++. The following steps are taken to solve the problem:

  1. Create and initialize an unordered_map called numberToIndex to store elements as keys and their corresponding indices as values.
  2. Create an empty vector called result to store the resulting indices.
  3. Iterate through the elements of the input vector 'numbers':
    • Calculate the complement by subtracting the current number from the target.
    • Check if the complement is already in the 'numberToIndex' map using numberToIndex.find(complement).
  4. If the complement is found:
    • Add the indices of the two numbers to the 'result' vector: the index of the complement (from the map) and the current index.
    • Return the 'result' vector and exit the function.
  5. If the complement is not found:
    • Add the current number and its index to the 'numberToIndex' map.
  6. If no solution is found after the entire loop, return an empty 'result' vector.

#12

Copy link
Owner

@iamdestinychild iamdestinychild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, everything looks good #12

@iamdestinychild iamdestinychild merged commit 0b0e97b into iamdestinychild:main Oct 2, 2023
2 checks passed
@Dhavisco
Copy link
Contributor Author

Dhavisco commented Oct 2, 2023

great, everything looks good #12

Thank you. :)
Up to the next one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants