My 4 lines C++ solution in 3ms


  • 0
    P
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int end = s.length(), res = 0;
            while (--end >= 0 && s[end] == ' ');
            while (end >= 0 && s[end--] != ' ') ++res;
            return res;
        }
    };
    

Log in to reply
 

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