Skip to content

Commit b8de49f

Browse files
committed
sovled #75
1 parent e7ed6cf commit b8de49f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

075.Sort Colors/coderfive/data

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{} ## {}
2+
{1,0,2} ## {0,1,2}
3+
{1,2} ## {1,2}
4+
{0,2} ## {0,2}
5+
{1,0} ## {0,1}
6+
{0,1,0} ## {0,0,1}
7+
{0,1,0,2} ## {0,0,1,2}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <vector>
2+
#include <string>
3+
#include <utility>
4+
#include <iostream>
5+
#include <algorithm>
6+
#include <limits>
7+
#include <numeric>
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
void sortColors(vector<int>& nums) {
13+
int arr[3] = {0, 0, 0};
14+
for (auto n : nums)
15+
arr[n]++;
16+
arr[1] += arr[0];
17+
std::fill (nums.begin(), nums.begin()+arr[0], 0);
18+
std::fill (nums.begin()+arr[0], nums.begin()+arr[1], 1);
19+
std::fill (nums.begin()+arr[1], nums.end(), 2);
20+
}
21+
};
22+
23+
/*
24+
* input: vector<int> ## vector<int>
25+
*/
26+
27+
template<typename T>
28+
istream& operator >> (istream& in, vector<T>& vi) {
29+
T val;
30+
vi.clear();
31+
while (!in.eof() && isspace(in.peek())) in.get();
32+
if (in.eof()) return in;
33+
in.get();
34+
35+
while (true) {
36+
while (isspace(in.peek())) in.get();
37+
if (in.peek() == '}') {
38+
in.get();
39+
return in;
40+
}
41+
else if (in.peek() == ',') in.get();
42+
in >> val;
43+
vi.push_back(val);
44+
}
45+
46+
return in;
47+
}
48+
49+
template<typename T>
50+
ostream& operator << (ostream& out, vector<T>& vi) {
51+
out << "{";
52+
if (vi.size() != 0)
53+
out << vi[0];
54+
for (int i = 1; i < vi.size(); i++)
55+
out << ", " << vi[i];
56+
out << "}";
57+
return out;
58+
}
59+
60+
int main() {
61+
bool err = false;
62+
string buf;
63+
vector<int> nums, expected, saved_input;
64+
65+
while (true) {
66+
cin >> nums;
67+
if (cin.eof()) break;
68+
cin >> buf >> expected;
69+
saved_input = nums;
70+
Solution().sortColors(nums);
71+
if (expected != nums) {
72+
err = true;
73+
cout << "input: " << saved_input << endl
74+
<< "result: " << nums << endl
75+
<< "expected: " << expected << endl << endl;
76+
}
77+
}
78+
79+
if (!err)
80+
cout << "All tests passed!\n";
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)