simple c++ 4ms solution using stringstream


  • 0
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            stringstream ss(s);
            vector<string> strs;
            string tmp;
            while(ss>>tmp)
                strs.push_back(tmp);
            if(strs.empty())
                return 0;
            return strs.back().size();
        }
    };

Log in to reply
 

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