# 58. Length of Last Word 最后一个单词的长度

@TOC

## # 题目描述

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.

Examle:

input:

``````"Hello World"
"Hello World "
"Hello W orld"
"Hello Wo   rld"
``````

output:

``````5
5
4
3
``````

## # 解题方法

### # 库函数

``````class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
return len(s.strip().split(' ')[-1])

``````

### # 双指针

Python代码如下：

``````class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
N = len(s)
left, right = 0, N - 1
while right >= 0 and s[right] == " ":
right -= 1
left = right
while left >= 0 and s[left] != " ":
left -= 1
return right - left
``````

C++代码如下：

``````class Solution {
public:
int lengthOfLastWord(string s) {
int N = s.size();
int left = 0, right = N - 1;
while (right >= 0 && s[right] == ' ') right--;
left = right;
while (left >= 0 && s[left] != ' ') left--;
return right - left;
}
};
``````

### # 单指针

Python版本如下：

``````class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
N = len(s)
count = 0
for i in range(N - 1, -1, -1):
if s[i] == " ":
if count == 0:
continue
else:
break
else:
count += 1
return count
``````

C++版本如下：

``````class Solution {
public:
int lengthOfLastWord(string s) {
int N = s.size();
int count = 0;
for (int i = N - 1; i >= 0; --i) {
if (s[i] == ' ') {
if (count != 0) {
break;
}
} else {
count++;
}
}
return count;
}
};
``````

## # 日期

2017 年 8 月 24 日 2018 年 11 月 24 日 —— 周日开始！一周就过去了～