Skip to content

Commit d400b2b

Browse files
committed
Day 68 Leet code problem
1 parent 7e42009 commit d400b2b

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

@

Lines changed: 0 additions & 12 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 95 |
10-
| Current Streak | 67 days |
11-
| Longest Streak | 67 ( August 17, 2015 - October 22, 2015 ) |
9+
| Total Problems | 96 |
10+
| Current Streak | 68 days |
11+
| Longest Streak | 68 ( August 17, 2015 - October 23, 2015 ) |
1212

1313
</center>
1414

@@ -172,3 +172,4 @@ Include contains single header implementation of data structures and some algori
172172
| Given a 2D matrix, with following properties <ul><li>Integers in each row are sorted in ascending from left to right.</li></ul><ul><li>Integers in each column are sorted in ascending from top to bottom.</ul></li>|[search2DII.cpp](leet_code_problems/search2DII.cpp)|
173173
| Given an unsorted integer array, find the first missing positive integer.Example: [1,2,0] should return 3 and [3,4,-1,1] should return 2. Expected time complexity O(n) and solution should use constant space| [firstMissingPositiveNum.cpp](leet_code_problems/firstMissingPositiveNum.cpp)|
174174
|Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example: Given [100, 4, 200, 1, 3, 2]. The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Algorithm should run in O(n) complexity.| [longestConsecutiveSeq.cpp](leet_code_problems/longestConsecutiveSeq.cpp)|
175+
|Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.| [mergeArrays.cpp](leet_code_problems/mergeArrays.cpp)

leet_code_problems/mergeLists.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
3+
* Note:
4+
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold
5+
* additional elements from nums2.
6+
* The number of elements initialized in nums1 and nums2 are m and n respectively.
7+
*/
8+
9+
10+
#include <iostream>
11+
#include <vector>
12+
using namespace std;
13+
14+
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
15+
int i = m-1;
16+
int j = n-1;
17+
int k = m + n - 1;
18+
while ( i >= 0 && j >= 0 ) {
19+
if ( nums1[i] > nums2[j] ) {
20+
nums1[k] = nums1[i];
21+
--i;
22+
} else {
23+
nums1[k] = nums2[j];
24+
--j;
25+
}
26+
--k;
27+
}
28+
while ( j >= 0 ) {
29+
nums1[k] = nums2[j];
30+
--j;
31+
--k;
32+
}
33+
}
34+
35+
void printVec(std::vector<int> & vec ) {
36+
for ( auto x : vec ) {
37+
std::cout << x << " " ;
38+
}
39+
std::cout << std::endl;
40+
}
41+
42+
int main()
43+
{
44+
std::vector<int> vec1 { 2, 4, 6, 8, 10, 0, 0, 0, 0, 0};
45+
std::vector<int> vec2 { 1, 2, 3, 4, 5 };
46+
merge(vec1, 5, vec2, 5 );
47+
printVec(vec1);
48+
return 0;
49+
}

0 commit comments

Comments
 (0)