58. 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.
For example,
Given s = “Hello World”,
return 5.
题意:
给定一个字符串s由大/小写字母和空格字符组成,返回字符串中最后一个单词的长度。如果最后一个单词不存在,返回0。
思路:
考虑特殊情况三种情况:
1.整个字符串为空时
2.字符串由无数的空格组成时
3.字符串最后以空格结尾时
1 | class Solution { |
Java Code
1 | class Solution { |