We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9c8e491 commit fb8ac1bCopy full SHA for fb8ac1b
2018.12.3-leetcode58/fish.md
@@ -0,0 +1,13 @@
1
+>给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
2
+>如果不存在最后一个单词,请返回 0 。
3
+>说明:一个单词是指由字母组成,但不包含任何空格的字符串。
4
+
5
+```
6
+public static int lengthOfLastWord(String str){
7
+ String trim = str.trim();
8
+ if (str.matches("[a-zA-Z ]*") && !trim.isEmpty()) {
9
+ int index = trim.lastIndexOf(' ');
10
+ return index < 0 ? trim.length() : trim.length() - index - 1;
11
+ }
12
+ return 0;
13
+}
0 commit comments