[LeetCode By Python]58. Length of Last Word
2018-01-28 20:51
375 查看
题目:
Given a string s consists of upper/lower-case alphabets and empty space characters
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
我用的字符串内置函数,只有两行代码,然而我发现提交后,虽然正确但时间花费太多
代码+测试:
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
lists = s.rstrip().split(' ')
return len(lists[-1])
a = Solution()
print a.lengthOfLastWord(" ")
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.
Example:
Input: "Hello World" Output: 5
我用的字符串内置函数,只有两行代码,然而我发现提交后,虽然正确但时间花费太多
代码+测试:
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
lists = s.rstrip().split(' ')
return len(lists[-1])
a = Solution()
print a.lengthOfLastWord(" ")
相关文章推荐
- [LeetCode By Python]27. Remove Element
- [LeetCode BY Python]155. Min Stack
- [LeetCode By python]35. Search Insert Position
- [LeetCode By Python]38. Count and Say
- [LeetCode By Python]107. Binary Tree Level Order Traversal II
- [LeetCode By Python]26. Remove Duplicates from Sorted Array
- [LeetCode By Python] 2 Add Two Number
- [LeetCode By Python] 21. Merge Two Sorted Lists
- [LeetCode By Python]171. Excel Sheet Column Number
- [leetCode By python]104. Maximum Depth of Binary Tree
- [LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
- [LeetCode By Python]118. Pascal's Triangle
- [LeetCode BY Python]169. Majority Element
- [LeetCode By Python]9. Palindrome Number
- [LeetCode By Python]83. Remove Duplicates from Sorted List
- [LeetCode BY Python]160. Intersection of Two Linked Lists
- [Leetcode By Python]69. Sqrt(x)
- [LeetCode By Python]172. Factorial Trailing Zeroes
- [leetCode By Python]100. Same Tree
- [LeetCode By Python]119. Pascal's Triangle II