forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
31 lines (29 loc) · 830 Bytes
/
Solution.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
class Codec {
public:
// Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string ans;
for (string s : strs) {
int size = s.size();
ans += string((const char*) &size, sizeof(size));
ans += s;
}
return ans;
}
// Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> ans;
int i = 0, n = s.size();
int size = 0;
while (i < n) {
memcpy(&size, s.data() + i, sizeof(size));
i += sizeof(size);
ans.push_back(s.substr(i, size));
i += size;
}
return ans;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.decode(codec.encode(strs));