Given A string s consists of upper/lower-case alphabets and empty space characters ‘ ‘ , return the length of LA St Word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given S = "Hello World" ,
Return 5 .
This question really cost Dickens, originally did not have much difficult topic, oneself originally to this question feeling really quite simple. However, many situations are not well thought out, causing frequent bugs in the code. Finally, finally the debug passed
My Code:
classSolution { Public: intLengthoflastword (Const Char*s) {if(strlen (s) = =0) return 0; intLength =strlen (s); intCount =0; while(Length-count-1>=0) { if(S[length-count-1] >='a'&& S[length-count-1] <='Z'|| S[length-count-1] >='A'&&s[length-count-1] <='Z') && Length-count-1>=0) {Count++; if(Length-count = =0|| S[length-count-1] ==' ') Break; } if(S[length-count-1] ==' ') {length--; Continue; } } returncount; }};
In reference to other people's code, really feel shame ah. The following solution is simple and intuitive
intLengthoflastword (Const Char*s) {intLen =0; while(*s) {if(*s++! =' ') ++Len; Else if(*s && *s! =' ') Len=0; } returnLen; }
Happyleetcode35:length of last Word