My solution c++


  • 0
    S
     int lengthOfLastWord(string s) {
    	int a = s.find_last_of(' ');
    	if (a == -1) return s.length(); 
    	if (a == s.length() - 1){
    		while (a >= 0 && s[a] == ' ')   a--;
    		if (a == 0)  return 1;
    		if (a < 0) 	 return 0; 
    		return lengthOfLastWord(s.substr(0, a+1));
    	}
    	return s.length() - a - 1;
    }

Log in to reply
 

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