Skip to content

Commit a90b2d6

Browse files
committed
Merge pull request xxg1413#36 from coderfive/master
solved 3 problems
2 parents a8b1246 + b8de49f commit a90b2d6

File tree

6 files changed

+292
-0
lines changed

6 files changed

+292
-0
lines changed

071.Simplify Path/coderfive/data

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/home/ ## /home
2+
/a/./c/ ## /a/c
3+
/a/./b/../../c/ ## /c
4+
/.. ## /
5+
./ ## .
6+
./.. ## ./..
7+
./../a/../ ## ./..
8+
./abc/.. ## .
9+
/../../../././././ ## /
10+
../././.././.././../ ## ../../../..
11+
././../../././.././ ## ./../../..
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
string simplifyPath(string path) {
13+
int i = 0, j = 0, size = path.size();
14+
for (; j < size; j++) {
15+
if (!(i > 0 && path[i-1] == '/' && path[j] == '/'))
16+
path[i++] = path[j];
17+
if (path[j] == '/') {
18+
while (j < size && path[j] == '/')
19+
j++;
20+
j--;
21+
if (j+2 < size && path[j+1] == '.' && path[j+2] == '.'
22+
&& (j+3 == size || path[j+3] == '/')) {
23+
if (prev_slash (path, i)) {
24+
path[i++] = '.';
25+
path[i++] = '.';
26+
}
27+
j += 2;
28+
}
29+
else if (j+1 < size && path[j+1] == '.'
30+
&& (j+2 == size || path[j+2] == '/')) {
31+
j += 1;
32+
}
33+
}
34+
}
35+
path.resize (i);
36+
if (i == 0) path.push_back ('/');
37+
if (path.size() != 1 && path.back() == '/')
38+
path.pop_back();
39+
return path;
40+
}
41+
int prev_slash (string& path, int& i) {
42+
int j = i-2;
43+
if (i == 1 && path[0] == '/') return 0;
44+
while (j >= 0 && path[j] != '/') j--;
45+
if (j < 0) return 1;
46+
else {
47+
if (j+3 < i && path[j+1] == '.' && path[j+2] == '.' && path[j+3] == '/')
48+
return 1;
49+
else
50+
i = j+1;
51+
}
52+
return 0;
53+
}
54+
};
55+
56+
/*
57+
* input: string_without_space ## string_without_space
58+
*/
59+
60+
int main() {
61+
int err = 0;
62+
string buf, path, ret, expected;
63+
64+
while (true) {
65+
cin >> path;
66+
if (cin.eof()) break;
67+
cin >> buf >> expected;
68+
ret = Solution().simplifyPath(path);
69+
if (ret != expected) {
70+
err = 1;
71+
cout << "input: '" << path << "'\n"
72+
<< "ret: '" << ret << "'\n"
73+
<< "expected: '" << expected << "'\n\n";
74+
}
75+
}
76+
77+
if (err == 0)
78+
cout << "All tests passed!\n";
79+
80+
return 0;
81+
}

074.Search a 2D Matrix/coderfive/data

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{}} 0 ## false
2+
3+
{{1,2},{3,4}} 5 ## false
4+
{{1,2},{3,4}} 0 ## false
5+
6+
{{1},{2}} 1 ## true
7+
{{1},{2}} 2 ## true
8+
{{1},{2}} 3 ## false
9+
{{1},{2}} 0 ## false
10+
11+
{{1,2},{3,4}} 1 ## true
12+
{{1,2},{3,4}} 2 ## true
13+
{{1,2},{3,4}} 3 ## true
14+
{{1,2},{3,4}} 4 ## true
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
13+
if (matrix.size() == 0 || matrix[0].size() == 0)
14+
return false;
15+
int i = 0, j = matrix.size(), mid;
16+
while (i < j) {
17+
mid = (i+j)/2;
18+
if (matrix[mid][0] > target)
19+
j = mid;
20+
else if (matrix[mid].back() < target)
21+
i = mid+1;
22+
else
23+
return std::binary_search (matrix[mid].begin(), matrix[mid].end(), target);
24+
}
25+
return false;
26+
}
27+
};
28+
29+
/*
30+
* input: vector<vector<int>> int ## true|false
31+
*/
32+
33+
template<typename T>
34+
istream& operator >> (istream& in, vector<T>& vi) {
35+
T val;
36+
vi.clear();
37+
while (!in.eof() && isspace(in.peek())) in.get();
38+
if (in.eof()) return in;
39+
in.get();
40+
41+
while (true) {
42+
while (isspace(in.peek())) in.get();
43+
if (in.peek() == '}') {
44+
in.get();
45+
return in;
46+
}
47+
else if (in.peek() == ',') in.get();
48+
in >> val;
49+
vi.push_back(val);
50+
}
51+
52+
return in;
53+
}
54+
55+
template<typename T>
56+
ostream& operator << (ostream& out, vector<T>& vi) {
57+
out << "{";
58+
if (vi.size() != 0)
59+
out << vi[0];
60+
for (int i = 1; i < vi.size(); i++)
61+
out << ", " << vi[i];
62+
out << "}";
63+
return out;
64+
}
65+
66+
67+
int main() {
68+
bool err = false;
69+
string buf;
70+
vector<vector<int>> matrix;
71+
int target;
72+
bool res, expected;
73+
cout.setf(std::ios_base::boolalpha);
74+
75+
while (true) {
76+
cin >> matrix;
77+
if (cin.eof()) break;
78+
cin >> target >> buf >> buf;
79+
if (buf == "true")
80+
expected = true;
81+
else
82+
expected = false;
83+
res = Solution().searchMatrix(matrix, target);
84+
if (res != expected) {
85+
err = true;
86+
cout << "input: " << matrix << " " << target << endl
87+
<< "result: " << res << endl
88+
<< "expected: " << expected << endl << endl;
89+
}
90+
}
91+
92+
if (!err)
93+
cout << "All tests passed!\n";
94+
95+
return 0;
96+
}

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)