Skip to content

Commit 018c984

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 290291f commit 018c984

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

0075_sort_colors/sort_colors.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
56
static inline void swap(int *a, int *b)
67
{
78
int tmp = *a;
89
*a = *b;
910
*b = tmp;
1011
}
1112

12-
static void sortColors(int* nums, int numsSize)
13+
void sortColors(int* nums, int numsSize)
1314
{
1415
int i, j = 0;
1516
for (i = 0; i < numsSize; i++) {

0075_sort_colors/sort_colors.cc

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
void sortColors(vector<int>& nums) {
8+
int i = 0, j = nums.size() - 1;
9+
while (i < j) {
10+
if (nums[i] == 0) {
11+
i++;
12+
continue;
13+
}
14+
if (nums[j] != 0) {
15+
j--;
16+
continue;
17+
}
18+
swap(nums[i], nums[j]);
19+
}
20+
21+
j = nums.size() - 1;
22+
while (i < j) {
23+
if (nums[i] == 1) {
24+
i++;
25+
continue;
26+
}
27+
if (nums[j] != 1) {
28+
j--;
29+
continue;
30+
}
31+
swap(nums[i], nums[j]);
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)