-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString58.java
More file actions
36 lines (35 loc) · 1.11 KB
/
String58.java
File metadata and controls
36 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package string;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String58
* @Author: markey
* @Description:58. 最后一个单词的长度
* 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度。如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。
*
* 如果不存在最后一个单词,请返回 0 。
*
* 说明:一个单词是指仅由字母组成、不包含任何空格字符的 最大子字符串。
*
*
*
* 示例:
*
* 输入: "Hello World"
* 输出: 5
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/length-of-last-word
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/4/27 22:56
* @Version: 1.0
*/
public class String58 {
public int lengthOfLastWord(String s) {
String[] res = s.trim().split(" ?");
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
return res.length == 0 ? 0 : res[res.length - 1].length();
}
}