题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
http://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
Python3 解题思路
- 利用python语法分割字符串为列表,返回列表末尾元素长度
import sys
s = input().split(" ")
n = len(s)-1
print(len(s[n]))
2.利用单指针从末尾开始遍历,遇到空格即返回
import sys
s = input().strip()
i = len(s)-1
count = 0
while i>-1:
if s[i] ==' ': break
i -= 1
count+=1
print(count)