原题链接:https://leetcode.com/problems/length-of-last-word/description/
这题目没啥难度,稍微懂点编程均可独立完成:
class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
char[] chars = s.toCharArray();
for (int i = chars.length - 1; i >= 0; i--) {
if (chars[i] == ‘ ‘) {
if (len > 0) {
return len;
}
} else {
len++;
}
}
return len;
}
}