C++ 3ms


  • 0
    J
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            if(s.size() == 0) return 0;
            int len = 0;
            int found = s.find_last_not_of(' '); 
            while(found > -1 && s[found] != ' ') {found--; len++;}
            return len;
        }
    };
    

Log in to reply
 

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