A straight forward c++ solution


  • 0
    C

    class Solution {
    public:
    int lengthOfLastWord(string s) {

        int space=-1,len=0;
        for(int i=0;s[i]!=0;i++){
            if(s[i]==' ') space=i;
            else len=i-space;
        }
        return len;
    }
    

    };


Log in to reply
 

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