letcode434 字符串中的单词数

https://leetcode-cn.com/problems/number-of-segments-in-a-string/
思路比较简单 直接判断单词间的空格就行
class Solution:
    def countSegments(self, s: str) -> int:
        flag = False
        ans = 0
        for i in range(len(s)):
            if s[i]!=' ':
                flag = True
            elif s[i]==' ' and flag==True:
                ans+=1
                flag = False
        if flag==True:
            ans+=1
        return ans


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务