forked from Doragd/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
字符串字符串字符串
Description
- 计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
- 输入一行,代表要计算的字符串,非空,长度小于5000。
- 输出一个整数,表示输入字符串最后一个单词的长度。
- 问题分解
- 拿到字符串
- 空格拆分得到数组
- 拿到数组最后一个 获取字符串长度
class Solution {
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] str = in.nextLine().split(" ");
System.out.println(indexOfLastString(str).length());
}
public static String indexOfLastString(String[] string){
return string[string.length-1];
}
}
};
Metadata
Metadata
Assignees
Labels
字符串字符串字符串