Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Codec {
- public:
- // Encodes a list of strings to a single string.
- string encode(vector<string>& strs) {
- string encoded = "";
- for (string &str: strs) {
- int len = str.size();
- encoded += to_string(len) + "@" + str;
- }
- return encoded;
- }
- // Decodes a single string to a list of strings.
- vector<string> decode(string s) {
- vector<string> r;
- int head = 0;
- while(head < s.size()) {
- int at_pos = s.find('@', head);
- int len = stoi(s.substr(head, at_pos - head));
- head = at_pos + 1;
- r.push_back(s.substr(head, len));
- head += len;
- }
- return r;
- }
- };
- // Your Codec object will be instantiated and called as such:
- // Codec codec;
- // codec.decode(codec.encode(strs));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement