Tuesday, September 9, 2014

Leetcode: Length of Last Word @Python

class Solution:
    # @param s, a string
    # @return an integer
    def lengthOfLastWord(self, s):
        l=s.split(' ')
        if len(l)==1:
            return len(l[-1])
        else:
            for i in reversed(range(len(l))):
                if l[i]!='':
                    return len(l[i])
            return 0

No comments :

Post a Comment