4 lines C++ 3ms Solution


  • 0
    B
    int lengthOfLastWord(string s) {
            int j = s.size()-1, end =j;
            while(s[j--]==' ') end--;
            while(j>=0){ if(s[j]==' ') return end-j; j--; }
            return end+1;
        }
    

    Though it is not really 4 lines, I see my solution is a bit different than other's.


Log in to reply
 

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