mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 05:24:07 +00:00
starting out: 88-merge-sorted-array
This commit is contained in:
commit
a9d7373732
39
88-merge-sorted-array/soln.cpp
Normal file
39
88-merge-sorted-array/soln.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <bits/stdc++.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
|
||||
{
|
||||
int arr1_p = 0, arr2_p = 0;
|
||||
vector<int> result = vector<int>(m + n);
|
||||
for (int i = 0; i < m + n; i++)
|
||||
{
|
||||
// second array depleted
|
||||
if (arr2_p >= n)
|
||||
{
|
||||
result[i] = nums1[arr1_p++];
|
||||
}
|
||||
// first array depleted
|
||||
else if (arr1_p >= m)
|
||||
{
|
||||
result[i] = nums2[arr2_p++];
|
||||
}
|
||||
// nums2 elem greater, take from nums1
|
||||
else if (nums2[arr2_p] > nums1[arr1_p])
|
||||
{
|
||||
result[i] = nums1[arr1_p++];
|
||||
}
|
||||
// nums1 elem greater, take from nums2
|
||||
else if (nums2[arr2_p] <= nums1[arr1_p])
|
||||
{
|
||||
result[i] = nums2[arr2_p++];
|
||||
}
|
||||
}
|
||||
nums1 = result;
|
||||
}
|
||||
};
|
||||
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# leetcode-gulag: self-coerced doses of daily leetcode
|
||||
|
||||
- My work on various Leetcode problems
|
||||
- Shoutout to [leetcode-torture](https://github.com/The-CodingSloth/haha-funny-leetcode-extension) for making me commit to this
|
||||
5
thoughts.md
Normal file
5
thoughts.md
Normal file
@ -0,0 +1,5 @@
|
||||
# misc. thoughts and maybe tips
|
||||
|
||||
- libraries:
|
||||
- essentials: bits/stdc++.h, iostream
|
||||
- data structures: vector
|
||||
Loading…
x
Reference in New Issue
Block a user