2114
一个 句子 由一些 单词 以及它们之间的单个空格组成,句子的开头和结尾不会有多余空格。
给你一个字符串数组 sentences ,其中 sentences[i] 表示单个 句子 。
请你返回单个句子里 单词的最多数目 。
输入:sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"] 输出:6 解释: - 第一个句子 "alice and bob love leetcode" 总共有 5 个单词。 - 第二个句子 "i think so too" 总共有 4 个单词。 - 第三个句子 "this is great thanks very much" 总共有 6 个单词。 所以,单个句子中有最多单词数的是第三个句子,总共有 6 个单词。
class Solution(object): def mostWordsFound(self, sentences): """ :type sentences: List[str] :rtype: int """ n=0 for s in sentences: n=max(n,s.count(' ')+1) return n

直接数空格
还有一种做法是split 之前某一道题
Work Hard
But do not forget to enjoy life😀
本文来自博客园,作者:YuhangLiuCE,转载请注明原文链接:https://www.cnblogs.com/YuhangLiuCE/p/17084239.html
posted on 2023-02-01 21:45 YuhangLiuCE 阅读(303) 评论(0) 收藏 举报
浙公网安备 33010602011771号