1.(字符串)-获取最后一个字符串及长度

如:' hello word is good  ',返回:good,4

python:

class Solution:
    def strc(self, s):
        end = len(s)
        #取到最后一个字母所在位置的后一位
        while end > 0 and s[end - 1] == ' ':
            end -= 1
        
        start = end
        #取到最后一个单词的首字母位置
        while start > 0 and s[start - 1] != ' ':
             start -= 1
        L=end - start
    
        return L,s[start:end]

s = Solution()
print(s.strc(' hello word is good '))
#(4, 'good')

 

posted @ 2019-02-12 21:35  jj千寻  阅读(196)  评论(0编辑  收藏  举报