设置一个变量显示是否读了新单词
若新单词,标识一下。
public class Solution {
public int lengthOfLastWord(String s) {
// Start typing your Java solution below
// DO NOT write main() function
int len = s.length();
if (len == 0)
return 0;
int i = 0;
int count = 0;
boolean is_new = true;
while (i < len) {
if (s.charAt(i) == ' ') {
is_new = true;
} else {
if (is_new == true) {
count = 1;
is_new = false;
} else
count++;
}
i++;
}
return count;
}
}