58. 最后一个单词的长度

题目链接:https://leetcode.cn/problems/length-of-last-word/description/

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int lengthOfLastWord(String s) {
int res = 0;
String ss = s.trim();
int n = ss.length();
while(n >= 1) {
if (ss.charAt(n-1) != ' ') {
res++;
n--;
} else {
break;
}
}
return res;
}
}

58. 最后一个单词的长度
https://pisces34.github.io/2024/02/26/leetcode/58/
发布于
2024年2月26日
许可协议