Code & Fun

第76天。

快考试,可能要水一个月的easy题了。

今天的题目是Length of Last Word:

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: “Hello World”
Output: 5

一看完题目,我就想到了pythonsplit:

1
2
3
4
5
6
7
8
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
words = s.split();
if len(words) == 0: return 0;
return len(words[-1])

然后是用c++find去解的:

1
2
3
4
5
6
7
8
9
10
11
12
13
int lengthOfLastWord(string s) {
auto beg = s.begin();
auto it = beg;
auto end = s.end();
// fix the bug like that "hello world "
for(int i = s.size() - 1;i >= 0 && s[i] == ' ';i--)
end--;

while((it = find(beg,end,' ')) != end) {
beg = it + 1;
}
return end - beg;
}

然后是从后面向前扫描的方法:

1
2
3
4
5
6
7
int lengthOfLastWord(string s) {
auto end = s.rbegin();
while(end != s.rend() && *end == ' ') end++;
auto beg = end;
while(beg != s.rend() && *beg != ' ') beg++;
return beg - end;
}

然后是dicuss中的解法,和上面的从后向前扫描的方法类似,只不过它第二个循环里面顺带计算了length:

1
2
3
4
5
6
7
8
9
int lengthOfLastWord(string s) { 
int len = 0, tail = s.length() - 1;
while (tail >= 0 && s[tail] == ' ') tail--;
while (tail >= 0 && s[tail] != ' ') {
len++;
tail--;
}
return len;
}

本文首发于Code & Fun