- Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathleetcode-playground-built-in-functions.cpp
326 lines (290 loc) · 6.79 KB
/
leetcode-playground-built-in-functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<cassert>
#include<sstream>/* 含有stringstream类 */
#include<iostream>
usingnamespacestd;
/**
* Definition for singly-linked list.
*/
structListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
/**
* Definition for a binary tree node.
*/
structTreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
voidtrimLeftTrailingSpaces(string &input)
{
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch)
{ return !isspace(ch); }));
}
voidtrimRightTrailingSpaces(string &input)
{
input.erase(find_if(input.rbegin(), input.rend(), [](int ch)
{ return !isspace(ch); })
.base(),
input.end());
}
vector<int> stringToIntegerVector(string input)
{
vector<int> output;
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
stringstream ss;
ss.str(input);
string item;
char delim = ',';
while (getline(ss, item, delim))
{
output.push_back(stoi(item));
}
return output;
}
intstringToInteger(string input)
{
returnstoi(input);
}
ListNode *stringToListNode(string input)
{
// Generate list from the input
vector<int> list = stringToIntegerVector(input);
// Now convert that list into linked list
ListNode *dummyRoot = newListNode(0);
ListNode *ptr = dummyRoot;
for (int item : list)
{
ptr->next = newListNode(item);
ptr = ptr->next;
}
ptr = dummyRoot->next;
delete dummyRoot;
return ptr;
}
string integerVectorToString(vector<int> list, int length = -1)
{
if (length == -1)
{
length = list.size();
}
if (length == 0)
{
return"[]";
}
string result;
for (intindex = 0; index < length; index++)
{
int number = list[index];
result += to_string(number) + ", ";
}
return"[" + result.substr(0, result.length() - 2) + "]";
}
string listNodeToString(ListNode *node)
{
if (node == nullptr)
{
return"[]";
}
string result;
while (node)
{
result += to_string(node->val) + ", ";
node = node->next;
}
return"[" + result.substr(0, result.length() - 2) + "]";
}
string boolToString(bool input)
{
return input ? "True" : "False";
}
boolstringToBool(string input)
{
transform(input.begin(), input.end(), input.begin(), ::tolower);
return input == "true";
}
string treeNodeToString(TreeNode *root)
{
if (root == nullptr)
{
return"[]";
}
string output = "";
queue<TreeNode *> q;
q.push(root);
while (!q.empty())
{
TreeNode *node = q.front();
q.pop();
if (node == nullptr)
{
output += "null, ";
continue;
}
output += to_string(node->val) + ", ";
q.push(node->left);
q.push(node->right);
}
return"[" + output.substr(0, output.length() - 2) + "]";
}
TreeNode *stringToTreeNode(string input)
{
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
if (!input.size())
{
returnnullptr;
}
string item;
stringstream ss;
ss.str(input);
getline(ss, item, ',');
TreeNode *root = newTreeNode(stoi(item));
queue<TreeNode *> nodeQueue;
nodeQueue.push(root);
while (true)
{
TreeNode *node = nodeQueue.front();
nodeQueue.pop();
if (!getline(ss, item, ','))
{
break;
}
trimLeftTrailingSpaces(item);
if (item != "null")
{
int leftNumber = stoi(item);
node->left = newTreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (!getline(ss, item, ','))
{
break;
}
trimLeftTrailingSpaces(item);
if (item != "null")
{
int rightNumber = stoi(item);
node->right = newTreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
string stringToString(string input)
{
assert(input.length() >= 2);
string result;
for (int i = 1; i < input.length() - 1; i++)
{
char currentChar = input[i];
if (input[i] == '\\')
{
char nextChar = input[i + 1];
switch (nextChar)
{
case'\"':
result.push_back('\"');
break;
case'/':
result.push_back('/');
break;
case'\\':
result.push_back('\\');
break;
case'b':
result.push_back('\b');
break;
case'f':
result.push_back('\f');
break;
case'r':
result.push_back('\r');
break;
case'n':
result.push_back('\n');
break;
case't':
result.push_back('\t');
break;
default:
break;
}
i++;
}
else
{
result.push_back(currentChar);
}
}
return result;
}
voidprettyPrintTree(TreeNode *node, string prefix = "", bool isLeft = true)
{
if (node == nullptr)
{
cout << "Empty tree";
return;
}
if (node->right)
{
prettyPrintTree(node->right, prefix + (isLeft ? "│ " : ""), false);
}
cout << prefix + (isLeft ? "└── " : "┌── ") + to_string(node->val) + "\n";
if (node->left)
{
prettyPrintTree(node->left, prefix + (isLeft ? "" : "│ "), true);
}
}
voidprettyPrintLinkedList(ListNode *node)
{
while (node && node->next)
{
cout << node->val << "->";
node = node->next;
}
if (node)
{
cout << node->val << endl;
}
else
{
cout << "Empty LinkedList" << endl;
}
}
classSolution {
public:
intlongestConsecutive(vector<int> &nums)
{
return0;
}
};
// Test
intmain()
{
string line;
while (getline(cin, line))
{
vector<int> nums = stringToIntegerVector(line);
int ret = Solution().longestConsecutive(nums);
string out = to_string(ret);
cout << out << endl;
}
return0;
}