-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from sumit-kushwah/master
two sum solved
- Loading branch information
Showing
2 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |