/
01 Two Sum.cpp
216 lines (185 loc) · 6.53 KB
/
01 Two Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// https://leetcode.com/problems/two-sum/discuss/1162422/Explained-with-pen-and-paper-and-gfg-ref
---------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> hmap;
for (int i=0;i<nums.size();i++){
if (hmap.count(target-nums[i]))
return {i,(hmap[target-nums[i]])};
hmap[nums[i]]=i;
}
return {};
}
};
------------------------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i=0;i<nums.size();i++){
for (int j=i+1;j<nums.size();j++){
int res= nums[i]+nums[j];
if (res==target)
return {i,j};
}
}
return {};
}
};
----------------------------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//Key is the number and value is its index in the vector.
unordered_map<int,int> hash;
vector<int> res;
for (int i=0;i<nums.size();++i)
{
int numbertofind=target-nums[i];
//if numberToFind is found in map, return them
if (hash.find(numbertofind)!=hash.end())
{
res.push_back(hash[numbertofind]); // take index from map where the target element is found
res.push_back(i);
return res;
}
hash[nums[i]]=i;
}
return res;
}
};
#include <vector>
#include <iostream>
using namespace std;
// ---------------------------------------------------------- O(n*m)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i=0;i<nums.size();i++){
for (int j=i+1;j<nums.size();j++){
int complement =target-nums[i];
if (complement == nums[j]){
return {i,j};
}
}
}
return {};
}
};
// -------------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n= nums.size();
unordered_map<int,int> hmap;
vector <int> ans;
for (int i=0;i<n;i++)
{
if (hmap.find(target-nums[i])!=hmap.end()){
ans.push_back(hmap[target-nums[i]]);
ans.push_back(i);
return ans;
}
hmap[nums[i]]=i; // store current nums of i in the hash table
}
return ans;
}
};
//---------------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> twoSumAns;
for (int i = 0; i < nums.size(); i++) {
for (int j = nums.size() - 1; j > i; j--) {
if (nums[i] + nums[j] == target) {
twoSumAns.push_back(i);
twoSumAns.push_back(j);
}
}
}
return twoSumAns;
}
};
void PrintVector(vector<int>& input) {
for (int i = 0; i < input.size(); i++) {
cout << input[i] << " ";
}
cout << endl;
}
int main() {
Solution solution;
vector<int> nums = {2,7,11,15};
int Sum = 9;
/* vector<int> nums = { 3,2,4 };
int Sum = 6;*/
vector<int> result = solution.twoSum(nums, Sum);
PrintVector(result);
}
-----------------------------------------------------------------------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int num1; // make a variable for finding the element
for (int i = 0; i < nums.size(); i++) // iterate through the vector
{
num1 = target - nums[i]; // we will get the 2nd item to bve find
for (int k = i+1; k < nums.size(); k++ ) // for finding the position
{
if(nums[k] == num1)
{
return {i,k}; // just take both the subscript
}
}
}
return {};
}
};
// ------------------------------------------------------------------------------------------------------------
class Solution {
public:
std::vector<int> twoSum(vector<int>& nums, int target) {
//map for finding the complement of current number in the vector
std::unordered_map<int, int> hashmap;
//loop for checking if the complement of the current integer exist in the map
for(int i = 0; i < nums.size(); i++){
//if complement has found, return current index and index of compliment
//index of complement is its value in the map
//with C++20, mappy.contains would probably been better
if(hashmap.count(target-nums[i])){
//return the index of the current integer in the vector, and the index of the complement
return {i,(hashmap[target - nums[i]])};
}
//if complement has not been passed by yet, modify the complement's value with its index
hashmap[nums[i]] = i;
}
//return empty vector if no integers answer found
return {};
}
};
------------------------------------------------------------------------------------------------------------------
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map <int,int> hmap;
for (int i=0;i<nums.size();i++)
{
if (hmap.count(target-nums[i]))
return {i,hmap[target-nums[i]]};
hmap[nums[i]]=i;
}
return {};
}
};
------------------------------------------------------------------------------------------------------------------------
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap();
for(int i = 0; i < nums.length; i++)
map.put(nums[i], i);
for(int i = 0; i < nums.length; i++)
if(map.containsKey(target - nums[i]) && i != map.get(target - nums[i]))
return new int[] {i, map.get(target - nums[i])};
return new int[2];
}
}