Skip to content

Commit 6545cfc

Browse files
authored
Merge pull request gzc426#9 from V1ncentzzZ/V1ncentzzZ-patch-6
Create 。V1ncentzzZ.md
2 parents b6fb6c5 + 1b8d9c1 commit 6545cfc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2018.12.2-leetcode58/。V1ncentzzZ.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
58. 最后一个单词的长度 https://leetcode-cn.com/problems/length-of-last-word/
3+
4+
给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
5+
6+
如果不存在最后一个单词,请返回 0 。
7+
8+
说明:一个单词是指由字母组成,但不包含任何空格的字符串。
9+
10+
示例:
11+
12+
输入: "Hello World"
13+
输出: 5
14+
15+
16+
class Solution {
17+
public int lengthOfLastWord(String s) {
18+
s = s.trim();
19+
int result = 0;
20+
if("".equals(s)) return result;
21+
if(!s.contains(" ")) return s.length();
22+
for(int i = s.lastIndexOf(" ")+1; i<s.length(); i++){
23+
result++;
24+
}
25+
return result;
26+
}
27+
}

0 commit comments

Comments
 (0)