-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday 2 problem leet code solutions
40 lines (32 loc) · 1.06 KB
/
day 2 problem leet code solutions
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
/*PROBLEM 1 - TWO ARRAY LEET CODE SOLUTION ABHISHEK TYAGI 0020((1923IT1200) */
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> m;
vector<int> v;
for(int i=0; i<nums.size(); i++)
{
if(m.find(target-nums[i] )!= m.end())
{
v.push_back(m[target-nums[i]]);
v.push_back(i);
return v;
}
m[nums[i]]=i;
}
return v;
}
};
/*PROBLEM 2- MERGE TWO ARRAY ABHISHEK TYAGI0020 (1923IT1200) */
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int r=m+n-1;// both array merge and subtract-1 value after merge array
int r1=m-1;//first array
int r2=n-1;//second array
while(r1>=0 && r2>=0) // r1 value greater then or equal to 0 and r2 greater then or equal 0
nums1[r--]=nums1[r1] > nums2[r2]?nums1[r1--]:nums2[r2--];//
while(r2>=0)
nums1[r--]=nums2[r2--];
}
};