C++ encode with '#'


  • 0
    class Codec {
    public:
    
        // Encodes a list of strings to a single string.
        string encode(vector<string>& strs) {
            string res = "";
            for(auto s: strs)
                res += to_string(s.size()) + '#' + s;
            return res;
        }
    
        // Decodes a single string to a list of strings.
        vector<string> decode(string s) {
            vector<string>res;
            for(int i = 0; i < s.size();){
                int j = i;
                while(isdigit(s[j])) j++;
                int len = stoi(s.substr(i, j - i));
                res.push_back(s.substr(j + 1, len));
                i = j + len + 1;
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.