这题别看简单,但是恶心啊。。。我粗心大意交错了好几次23333
class Solution {
public int lengthOfLastWord(String s) {
if (s.equals("") || s.equals(" ")) {
return 0;
}
int index = s.lastIndexOf(' ');
while (index == s.length() - 1 && index >= 0) {
s = s.substring(0, index);
index = s.lastIndexOf(' ');
}
if (index == -1) {
return s.length();
}
return s.length() - s.lastIndexOf(' ') - 1;
}
}