Skip to content

Commit

Permalink
Merge pull request #2 from sumit-kushwah/master
Browse files Browse the repository at this point in the history
two sum solved
  • Loading branch information
17mi540 authored Oct 9, 2021
2 parents 17d728f + 103b0b9 commit 6eff20e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# leetcode
Solution of leetcode problems
# Leetcode
This repository contains my solutions of [leetcode](https://leetcode.com/problems) problems.

Solutions are available only in `c++` programming language.

Each file named as `problem_name_on_leetcode.cpp`.

***Note: Leetcode is a great platform to improve problem solving skills.***
35 changes: 35 additions & 0 deletions solutions/two_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

class Solution
{
public:
// write your code here
vector<int> twoSum(vector<int> &nums, int target)
{
unordered_map<int, int> umap;
vector<int> ans;
for (int i = 0; i < nums.size(); i++)
umap[nums[i]] = i;
for (int i = 0; i < nums.size(); i++)
{
int rem = target - nums[i];
if (umap.find(rem) != umap.end() && umap[rem] > i)
{
ans.push_back(i);
ans.push_back(umap[rem]);
return ans;
}
}
ans.push_back(-1);
ans.push_back(-1);
return ans;
}
};

int main()
{
Solution s;
}

0 comments on commit 6eff20e

Please sign in to comment.