LeetCode58: Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last 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.
Example:
Input: "Hello World" Output: 5
非常简单的一个题,就是找一句英文话里的最后一个单词的长度,而且区分单词只需要看是否有空格隔开就行。思路是遍历单词非空格时就计数,直到遇到空格为止,把计数结果保存到结果中,然后清空计数变量的值,重新计数下一个单词的长度,每一次如果计数值非0,说明之前保存的结果并非最后一个单词的长度,就把当前计数值赋给结果就行了。