Very simple C++ istringstream solutions


  • 0

    Solution 1: Just go through all words. Could it be any simpler?

    int lengthOfLastWord(string s) {
        string word;
        istringstream in(s);
        while (in >> word) {}
        return word.size();
    }
    

    Solution 2: Use the first word of the reversed string.

    int lengthOfLastWord(string s) {
        string r(s.rbegin(), s.rend()), word;
        istringstream in(r);
        in >> word;
        return word.size();
    }

Log in to reply
 

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