文章

58 length-of-last-word

58 length-of-last-word

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Example 1:

Input: s = “Hello World” Output: 5 Explanation: The last word is “World” with length 5. Example 2:

Input: s = “ fly me to the moon “ Output: 4 Explanation: The last word is “moon” with length 4. Example 3:

Input: s = “luffy is still joyboy” Output: 6 Explanation: The last word is “joyboy” with length 6.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        # cut the space and reverse the
        s = s.strip()[::-1]
        res = 0
        for i in s:
            if i != ' ':
                res += 1
            else:
                break
        return res

Time Complexity: O(n) Space Complexity: O(1)

本文由作者按照 CC BY 4.0 进行授权