My C solution,easy to understand.


  • 0
    W
    int lengthOfLastWord(char* s) {
    	int Len = strlen(s);
    	int length = 0,ans;
    	if (Len==0)
    	    return 0;
    	for (int i = Len-1; i >= 0; i--) {
    		if (s[i]!=' ') {
    			ans = i;
    			break;
    		}
    	}
    	for (int i = ans; i >= 0; i--) {
    		if (s[i] == ' ')
    			break;
    		length++;
    	}
    	return length;
    }
    

Log in to reply
 

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