A quasi c++ string.split function tailored for this problem


  • 0
    S

    One step to solve this problem was to split the input string to a vector of symbols to represent each a leaf or a none-leaf.

    The more general split function in C++ by https://leetcode.com/discuss/63986/c-solution-with-implemented-split-function was like this:

    vector<string> split(string s, char delimiter) {
        vector<string> result;
        stringstream ss(s);
        string tok;
        while (getline(ss, tok, delimiter)) {
            result.push_back(tok);
        }
        return result;
    }
    

    As far as this problem is concerned, my implementation was given below:

    vector<char> quasi_split(string& s, char delim) {
        vector<char> result;
        int sz = s.size();
        if (sz == 0)
            return result;
        
        for(int i = 1; i < sz; i++) {
            if (s[i] == delim)
                result.push_back(s[i-1]);
        }
        
        result.push_back(s[sz-1]);
        
        return result;
    }
    

    Although it may improve the efficiency a little, it makes the code less readable.

    So my confusion is, which one should we encourage?

    Thank you.


Log in to reply
 

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