题目描述:
LeetCode 434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
For example,
Input: "Hello, my name is John"
Output: 5
题目大意:
计算字符串中的非空子串的个数。
解题思路:
用内置函数split即可
Python代码:
class Solution(object):
def countSegments(self, s):
"""
:type s: str
:rtype: int
"""
return len(s.split())
本文链接:http://bookshadow.com/weblog/2016/12/04/leetcode-number-of-words-in-a-string/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。