题目:2114.句子中的最多单词数

​​题目来源:

        leetcode题目,网址:2114. 句子中的最多单词数 - 力扣(LeetCode)

解题思路:

       遍历数组,获得每个字符串中的单词数量,然后返回最多的单词数量即可。

解题代码:

class Solution {
    public int mostWordsFound(String[] sentences) {
        int res=0;
        for(String sentence:sentences){
            String[] split=sentence.split(" ");
            res=Math.max(split.length,res);
        }
        return res;
    }
}

总结:

        官方题解是计算每个字符串中的空格数量,单词数量=空格数量+1,然后返回最多单词数量。


你可能感兴趣的:(#,java,leetcode,java)