Skip to content

Commit e66c023

Browse files
committed
add LeetCodeInputTemplate
1 parent d877b0e commit e66c023

File tree

4 files changed

+150
-7
lines changed

4 files changed

+150
-7
lines changed

leetcode/.template/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ author: [email protected]
33
*/
44

55
#include <bits/stdc++.h>
6-
#include "../../lib/LeetCodeInput.hpp"
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
77
#include "solution.cpp"
88
using namespace std;
99

10+
typedef vector<int> vi;
11+
typedef vector<vector<int>> vvi;
12+
1013
int main()
1114
{
1215
LeetCodeInput li("0.in");
1316

14-
// vector<vector<int>> mat_1 = li.get_vvi(0);
15-
// vector<int> arr_1 = li.get_vi(0);
16-
// int num_1 = li.get_i(0);
17-
// string s_1 = li.get_s(0);
17+
auto l0 = li.get<vvi>(0);
1818

1919
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
2020
Solution solution;
21-
auto output = solution.solution();
21+
auto output = solution.solution(l0);
2222
chrono::steady_clock::time_point end = chrono::steady_clock::now();
2323
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
2424

leetcode/.template/solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef vector<vector<int>> vvi;
1717

1818
class Solution {
1919
public:
20-
int solution() {
20+
int solution(vvi l0) {
2121

2222
}
2323
};

lib/LeetCodeInput.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
3+
depreciated, use LeetCodeInputTemplate
34
*/
45

56
#include <bits/stdc++.h>
@@ -14,14 +15,19 @@ class LeetCodeInput{
1415
vector<string> r2vr(string& r);
1516
vector<vector<string>> r2vvr(string& r);
1617
vector<int> vr2vi(vector<string>& vr);
18+
vector<char> vr2vc(vector<string>& vr);
1719
vector<string> vr2vs(vector<string>& vr);
1820
vector<vector<int>> vvr2vvi(vector<vector<string>>& vvr);
21+
vector<vector<char>> vvr2vvc(vector<vector<string>>& vvr);
1922
vector<vector<string>> vvr2vvs(vector<vector<string>>& vvr);
2023

2124
int r2i(string& r);
25+
char r2c(string& r);
2226
string r2s(string& r);
2327
vector<int> r2vi(string& r);
28+
vector<char> r2vc(string& r);
2429
vector<vector<int>> r2vvi(string& r);
30+
vector<vector<char>> r2vvc(string& r);
2531
vector<string> r2vs(string& r);
2632
vector<vector<string>> r2vvs(string& r);
2733
public:
@@ -35,6 +41,7 @@ class LeetCodeInput{
3541
string get_s(int line) {return r2s(lines[line]);};
3642
vector<int> get_vi(int line) {return r2vi(lines[line]);};
3743
vector<vector<int>> get_vvi(int line) {return r2vvi(lines[line]);};
44+
vector<vector<char>> get_vvc(int line) {return r2vvc(lines[line]);};
3845
vector<string> get_vs(int line) {return r2vs(lines[line]);};
3946
vector<vector<string>> get_vvs(int line) {return r2vvs(lines[line]);};
4047
};
@@ -85,6 +92,10 @@ int LeetCodeInput::r2i(string& r){
8592
return stoi(r);
8693
}
8794

95+
char LeetCodeInput::r2c(string& r){
96+
return r[1];
97+
}
98+
8899
string LeetCodeInput::r2s(string& r){
89100
return r.substr(1, r.size() - 2);;
90101
}
@@ -103,6 +114,15 @@ vector<int> LeetCodeInput::vr2vi(vector<string>& vr){
103114
return vi;
104115
}
105116

117+
vector<char> LeetCodeInput::vr2vc(vector<string>& vr){
118+
int n = vr.size();
119+
vector<char> vc(n);
120+
for (int i=0; i<n; ++i){
121+
vc[i] = r2c(vr[i]);
122+
}
123+
return vc;
124+
}
125+
106126
vector<string> LeetCodeInput::vr2vs(vector<string>& vr){
107127
int n = vr.size();
108128
vector<string> vs(n);
@@ -132,6 +152,15 @@ vector<vector<int>> LeetCodeInput::vvr2vvi(vector<vector<string>>& vvr){
132152
return vvi;
133153
}
134154

155+
vector<vector<char>> LeetCodeInput::vvr2vvc(vector<vector<string>>& vvr){
156+
int n = vvr.size();
157+
vector<vector<char>> vvc(n);
158+
for (int i=0; i<n; ++i){
159+
vvc[i] = vr2vc(vvr[i]);
160+
}
161+
return vvc;
162+
}
163+
135164
vector<vector<string>> LeetCodeInput::vvr2vvs(vector<vector<string>>& vvr){
136165
int n = vvr.size();
137166
vector<vector<string>> vvs(n);
@@ -151,6 +180,11 @@ vector<vector<int>> LeetCodeInput::r2vvi(string& r){
151180
return vvr2vvi(vvr);
152181
}
153182

183+
vector<vector<char>> LeetCodeInput::r2vvc(string& r){
184+
auto vvr = r2vvr(r);
185+
return vvr2vvc(vvr);
186+
}
187+
154188
vector<string> LeetCodeInput::r2vs(string& r){
155189
auto vr = r2vr(r);
156190
return vr2vs(vr);

lib/LeetCodeInputTemplate.hpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
template <class T>
10+
struct is_std_vector { static const bool value=false; };
11+
12+
template <class T>
13+
struct is_std_vector<std::vector<T>> { static const bool value=true; };
14+
15+
class LeetCodeInput{
16+
private:
17+
vector<string> get_lines(string &path);
18+
vector<string> split(const string &s);
19+
20+
template <class T>
21+
T r2t(string& r);
22+
23+
public:
24+
vector<string> lines;
25+
26+
LeetCodeInput(string path){
27+
lines = get_lines(path);
28+
}
29+
30+
template <class T>
31+
T get(int line) {return r2t<T>(lines[line]);};
32+
33+
};
34+
35+
vector<string> LeetCodeInput::get_lines(string &path){
36+
string line;
37+
vector<string> lines;
38+
ifstream input(path);
39+
if (input.is_open())
40+
{
41+
while (getline(input, line))
42+
{
43+
lines.push_back(line);
44+
}
45+
input.close();
46+
}
47+
return lines;
48+
}
49+
50+
vector<string> LeetCodeInput::split (const string &s) {
51+
vector<string> result;
52+
int level=0;
53+
if (s == "[]"){
54+
return result;
55+
} else {
56+
result.push_back("");
57+
}
58+
for (auto c : s){
59+
switch(c){
60+
case '[':
61+
if (level != 0) {
62+
result.back().push_back(c);
63+
}
64+
++level;
65+
break;
66+
case ']':
67+
if (level != 0) {
68+
result.back().push_back(c);
69+
}
70+
--level;
71+
break;
72+
case ',':
73+
if (level == 1) {
74+
result.push_back("");
75+
} else {
76+
result.back().push_back(c);
77+
}
78+
break;
79+
default:
80+
result.back().push_back(c);
81+
break;
82+
}
83+
}
84+
return result;
85+
}
86+
87+
template <>
88+
int LeetCodeInput::r2t(string& r){
89+
return stoi(r);
90+
}
91+
92+
template <>
93+
char LeetCodeInput::r2t(string& r){
94+
return r[1];
95+
}
96+
97+
template <>
98+
string LeetCodeInput::r2t(string& r){
99+
return r.substr(1, r.size() - 2);
100+
}
101+
102+
template <class T>
103+
T LeetCodeInput::r2t(string& r){
104+
T res;
105+
for (auto e : split(r)){
106+
res.push_back(r2t<typename T::value_type>(e));
107+
}
108+
return res;
109+
}

0 commit comments

Comments
 (0)