GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-cli-plugins/docs/solution.discuss.md at master · skygragon/leetcode-cli-plugins

Expand file tree

/

solution.discuss.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
39 lines (29 loc) · 1.08 KB
/

solution.discuss.md

Copy path

File metadata and controls

39 lines (29 loc) · 1.08 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

solution.discuss

Fetch the most voted solution in discussion topics.

  • show + --solution to display most voted solution.
  • -l java to fetch java solution.

Usage

$ leetcode show 1 --solution

Accepted C++ O(n) Solution

https://discuss.leetcode.com/topic/3294/accepted-c-o-n-solution

* Lang:  cpp
* Votes: 221

vector<int> twoSum(vector<int> &numbers, int target)
{
    //Key is the number and value is its index in the vector.
    unordered_map<int, int> hash;
    vector<int> result;
    for (int i = 0; i < numbers.size(); i++) {
        int numberToFind = target - numbers[i];

            //if numberToFind is found in map, return them
        if (hash.find(numberToFind) != hash.end()) {
                    //+1 because indices are NOT zero based
            result.push_back(hash[numberToFind] + 1);
            result.push_back(i + 1);
            return result;
        }

            //number was not found. Put it in the map.
        hash[numbers[i]] = i;
    }
    return result;
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.