My C++ solution


  • 0
    C
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int n=s.size();
            int count =0;
            for(int i=0;i<n;){
                while(i<n&&s[i]!=' '){count++;i++;}
                while(i<n&&s[i]==' ')i++;
                if(i<n)count=0;
            }
            return count;
        }
    };

Log in to reply
 

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