php网站建设与维护,网站的设计特点有哪些,昆明专业网站营销,深圳官方网站建设一个 句子 由一些 单词 以及它们之间的单个空格组成#xff0c;句子的开头和结尾不会有多余空格。
给你一个字符串数组 sentences #xff0c;其中 sentences[i] 表示单个 句子 。
请你返回单个句子里 单词的最多数目 。 示例 1#xff1a;
输入#xff1a;sentences […一个 句子 由一些 单词 以及它们之间的单个空格组成句子的开头和结尾不会有多余空格。
给你一个字符串数组 sentences 其中 sentences[i] 表示单个 句子 。
请你返回单个句子里 单词的最多数目 。 示例 1
输入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 个单词。示例 2
输入sentences [please wait, continue to fight, continue to win]
输出3
解释可能有多个句子有相同单词数。
这个例子中第二个句子和第三个句子加粗斜体有相同数目的单词数。提示
1 sentences.length 1001 sentences[i].length 100sentences[i] 只包含小写英文字母和 。sentences[i] 的开头和结尾都没有空格。sentences[i] 中所有单词由单个空格隔开。 public static int mostWordsFound(String[] sentences) {int maxWords 0;for (String sentence : sentences) {int words 1;for (int i 0; i sentence.length(); i) {if (sentence.charAt(i) ) {words;}}maxWords Math.max(maxWords, words);}return maxWords;}