58. Length of Last Word
Description
描述:https://leetcode.com/problems/length-of-last-word/description/
题意:返回字符串中最后一个单词的长度
Solution: (Java)
class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
boolean find = false;
for (int i = s.length()-1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
if (find)
break;
} else {
len++;
find = true;
}
}
return len;
}
}
思路
- 本题比较简单,从字符串的最后一个字符开始遍历,runtime 0ms,超过100%