My C++ solution 4ms 5 lines


  • 0
    H
    class Solution {
    public:
        int lengthOfLastWord(const char *s) {
            string A(s);
            while(A[A.size()-1]==' ')
                A.erase(A.size()-1,1);
            if(A.empty())return 0;
            return A.rfind(" ")==A.npos?A.size():A.size()-A.rfind(" ")-1;
        }
    };
    

    :)


Log in to reply
 

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