Accepted C++ Solution - concise and simple - beats 80% of submissions


  • 0
    A

    Using a new line character as a separator between strings.

    class Codec {
    public:
    
        // Encodes a list of strings to a single string.
        string encode(vector<string>& strs) {
            string s="";
            for(int i=0;i<strs.size();i++)
            {
                s+=strs[i];
                s+='\n';
            }
            return s;
        }
    
        // Decodes a single string to a list of strings.
        vector<string> decode(string s) {
            vector<string> strs;
            
            for(int i =0;i<s.size();i++)
            {
                string temp = "";
                while(s[i]!='\n' && i<s.size())
                {
                    temp+=s[i++];
                }
                strs.push_back(temp);
            }
            return strs;
        }
    };
    

Log in to reply
 

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