My C++ solution


  • 0
    B
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int s_len = s.length();
            while(s_len>0 && s[s_len-1]==' ') {s_len--;}
            int len = 0;
            while(s_len>len && s[s_len-len-1]!=' ') {len++;}
            return len;
        }
    };

Log in to reply
 

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