-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
29 lines (23 loc) · 726 Bytes
/
Copy pathmain.cpp
File metadata and controls
29 lines (23 loc) · 726 Bytes
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
class Solution {
public:
string encode(vector<string>& strs) {
string result;
for (auto &str : strs) {
result += to_string(str.size()) + "#" + str;
}
return result;
}
vector<string> decode(string s) {
vector<string> strs;
int i = 0;
while (i < (int)s.size()) {
int j = i;
while (s[j] != '#') j++;
int length = stoi(s.substr(i, j - i));
string word = s.substr(j + 1, length);
strs.push_back(word);
i = j + 1 + length;
}
return strs;
}
};