An easy 4ms C++ Solution


  • 0
    Y
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int len = s.length();
            if(!len) return 0;
            vector<char> word;
            for(int i = len - 1; i >= 0 ; i--) {
                if(s[i] != ' ') word.push_back(s[i]);
                if((s[i] == ' ')&&(word.size() != 0)) return word.size();
            }
            return word.size();
        }
    };

Log in to reply
 

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