1243. 字符串中的单词数

1243. 字符串中的单词数

中文English

计算字符串中的单词数,其中一个单词定义为不含空格的连续字符串。

样例

样例:

输入: "Hello, my name is John"
输出: 5
解释:有五个字符串段落:"Hello"、"my"、"name"、"is"、"John"

注意事项

字符串中不包含任何 无法打印 的字符.

class Solution:
    """
    @param s: a string
    @return: the number of segments in a string
    """
    '''
    大致思路:
    1.以空格进行切割,循环列表,如果列表不为空,则count += 1,最后返回count
    '''
    def countSegments(self,s):
        dic_s = s.split(' ')
        count = 0
        for i in dic_s:
            if i != '':
                count += 1
        return count
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12733627.html