5946:句子中的最多单词数

This commit is contained in:
huangge1199@hotmail.com 2021-12-26 14:40:58 +08:00
parent 8d37ad30f6
commit 1518038a1e
5 changed files with 104 additions and 3 deletions

View File

@ -103,4 +103,3 @@ public class Solution273 {
}
}
}
}

View File

@ -0,0 +1,63 @@
//一个 句子 由一些 单词 以及它们之间的单个空格组成句子的开头和结尾不会有多余空格
//
// 给你一个字符串数组 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 <= 100
// 1 <= sentences[i].length <= 100
// sentences[i] 只包含小写英文字母和 ' '
// sentences[i] 的开头和结尾都没有空格
// sentences[i] 中所有单词由单个空格隔开
//
// 👍 1 👎 0
package leetcode.editor.cn;
//5946:句子中的最多单词数
class MaximumNumberOfWordsFoundInSentences{
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumNumberOfWordsFoundInSentences().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int mostWordsFound(String[] sentences) {
int max = 0;
for (String str : sentences) {
max = Math.max(str.split(" ").length, max);
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,39 @@
<p>一个 <strong>句子</strong>&nbsp;由一些 <strong>单词</strong>&nbsp;以及它们之间的单个空格组成,句子的开头和结尾不会有多余空格。</p>
<p>给你一个字符串数组&nbsp;<code>sentences</code>&nbsp;,其中&nbsp;<code>sentences[i]</code>&nbsp;表示单个 <strong>句子</strong>&nbsp;</p>
<p>请你返回单个句子里 <strong>单词的最多数目</strong>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>sentences = ["alice and bob love leetcode", "i think so too", <em><strong>"this is great thanks very much"</strong></em>]
<b>输出:</b>6
<b>解释:</b>
- 第一个句子 "alice and bob love leetcode" 总共有 5 个单词。
- 第二个句子 "i think so too" 总共有 4 个单词。
- 第三个句子 "this is great thanks very much" 总共有 6 个单词。
所以,单个句子中有最多单词数的是第三个句子,总共有 6 个单词。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>sentences = ["please wait", <em><strong>"continue to fight"</strong></em>, <em><strong>"continue to win"</strong></em>]
<b>输出:</b>3
<b>解释:</b>可能有多个句子有相同单词数。
这个例子中,第二个句子和第三个句子(加粗斜体)有相同数目的单词数。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= sentences.length &lt;= 100</code></li>
<li><code>1 &lt;= sentences[i].length &lt;= 100</code></li>
<li><code>sentences[i]</code>&nbsp;只包含小写英文字母和&nbsp;<code>' '</code>&nbsp;</li>
<li><code>sentences[i]</code>&nbsp;的开头和结尾都没有空格。</li>
<li><code>sentences[i]</code>&nbsp;中所有单词由单个空格隔开。</li>
</ul>
<div><li>👍 1</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long