Skip to content

Commit d6553e9

Browse files
authored
Merge pull request neetcode-gh#1867 from ektagoel-12/main
Create 0075-Sort-colors.cpp
2 parents e7c79e8 + cbc1d8f commit d6553e9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

cpp/0075-Sort-colors.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
void sortColors(vector<int>& nums) {
4+
int p1=0,p2=nums.size()-1;
5+
for(int i=p1;i<=p2;i++)
6+
{
7+
if(nums[i]==0)
8+
{
9+
swap(nums[i],nums[p1]);
10+
p1++;
11+
}
12+
if(nums[i]==2)
13+
{
14+
swap(nums[i],nums[p2]);
15+
p2--;
16+
i--;
17+
}
18+
}
19+
20+
21+
}
22+
};

cpp/0088-Merge-sorted-array.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
4+
int j=0;
5+
int i=0;
6+
if(n==0) return;
7+
if(m==0)
8+
{
9+
for(int i = 0; i < n; i++){
10+
nums1[i] = nums2[i];
11+
} return;
12+
}
13+
while(i<m)
14+
{
15+
if(nums1[i]>nums2[j])
16+
{
17+
swap(nums1[i],nums2[j]);
18+
sort(nums2.begin(),nums2.end());
19+
}
20+
i++;
21+
}
22+
j=0;
23+
while(i<m+n)
24+
{
25+
nums1[i] = nums2[j];
26+
j++;
27+
i++;
28+
}
29+
30+
}
31+
};

0 commit comments

Comments
 (0)