2021 E3 算法题第一题(Max Sentence Length)

news/2024/9/25 7:23:58/

题目内容

You would like to find the sentence containing the largest number of words in some given text. The text is specified as a string S consisting of N characters: letters, spaces, dots (.), question marks (?) and exclamation marks (!).The text can be divided into sentences by splitting it at dots, question marks and exclamation marks. A sentence can be divided into words by splitting it at spaces. A sentence without words is valid, but a valid word must contain at least one letter.For example, given S = "We test coders. Give us a try?", there are three sentences: "We test coders"," Give us a try" and "". 
The first sentence contains three words: "We", "test" and "coders". 
The second sentence contains four words: "Give", "us", "a" and "try". 
The third sentence is empty.Write a function:class Solution { public int solution(String $); }that, given a string S consisting of N characters, returns the maximum number of words in a sentence.For example, given S = "We test coders. Give us a try?", the function should return 4, as explained above.Given S="Forget CVs..Save time . x x", the function should return 2, as there are four sentences: "Forget CVs" (2 words), "" (0 words), "Save time "(2 words) and" x x" (2 words).Assume that:•  the length of S is within the range [1..100];•  string S consists only of letters (a-z, A-Z), spaces, dots (.), question marks (?) and exclamation marks (!).In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

解法一

思路

先把文本字分句子。然后遍历每个句子分词,分词之后比较每个句子的中词的个数,最后返回最大的词的数量。

java代码实现

import java.util.ArrayList;
import java.util.List;
public class Task1 {private List divideIntoSentences(String text){List<String> sentences = new ArrayList<>();char[] chars =  text.toCharArray();int startIndex = 0;int endIndex =0 ;for(int i=0; i<chars.length; i++){if ('.' == chars[i] || '!' == chars[i] || '?'==chars[i]){endIndex = i;sentences.add(text.substring(startIndex, endIndex));startIndex = endIndex+1;}}sentences.add(text.substring(startIndex-1, chars.length-1));return sentences;}public int solution(String text){if (text.length()==0){return 0;}List<String> sentences = divideIntoSentences(text);int maxLength = 0;int maxLengthInCurSentence;for (String sentence :sentences){maxLengthInCurSentence =0;String[] words = sentence.split(" ");for(int i=0; i< words.length; i++){if (words[i].length()!=0){maxLengthInCurSentence ++;}}if (maxLength < maxLengthInCurSentence){maxLength = maxLengthInCurSentence;}}return maxLength;}public static void main(String[] args) {Task1 task = new Task1();System.out.println(task.solution("We test coders. Give us a try?"));  // Output: 4System.out.println(task.solution("Forget CVs..Save time . x x"));  // Output: 2}}


http://www.ppmy.cn/news/1438340.html

相关文章

【GoWeb框架初探——GRPC】

1. GRPC介绍 1.1 什么是RPC RPC全程是Remote Procedure Call&#xff0c;远程过程调用。这是一种协议&#xff0c;是用来屏蔽分布式计算中的各种调用细节&#xff0c;使得你可以像是本地调用一样直接调用一个远程的函数。 调用流程 1&#xff09;客户端发送数据&#xff08;…

Pulsar Meetup 深圳 2024 会务介绍

“ Hi&#xff0c;各位热爱 Pulsar 的小伙伴们&#xff0c;Pulsar Meetup 深圳 2024 报名倒计时啦&#xff0c;快来报名。这里汇集了腾讯、华为和谙流科技等大量 Pulsar 大咖&#xff0c;干货多多&#xff0c;礼品多多&#xff0c;不容错过啊。 ” 活动介绍 由 AscentStream 谙…

.NET WinForm开放中的 窗体的 Designer.cs作用

一般窗体窗体 在资源管理器中会呈现 xxx.cs xxx.Designer.cs xxx.resx 》》》 .resx 是存放资源文件的&#xff0c;没啥好说的 xxx.cs 和 xxx.Designer.cs 都是partial类&#xff0c;而他们类名是一样的&#xff0c;所以在编译会生成一个文件。 xxx.Designer.cs 代码中有两…

MYSQL之锁机制

什么是锁机制? MySQL的锁机制是数据库中用于管理和控制对共享资源并发访问的一种机制。在多用户环境下&#xff0c;不同的用户可能同时对同一数据进行读写操作&#xff0c;如果没有适当的锁机制&#xff0c;就可能出现数据不一致或脏读等问题。 锁分类 1.从数据库的操作类型…

设计模式- 中介者模式(Mediator)

1. 概念 中介者模式&#xff08;Mediator Pattern&#xff09;&#xff0c;是一种对象行为型模式。该模式的主要目的是定义一个中介对象来封装一系列对象之间的交互&#xff0c;使原有对象之间的耦合变得松散&#xff0c;并且可以独立地改变它们之间的交互。 2. 原理结构图 抽…

XiaodiSec day019 Learn Note 小迪安全学习笔记

XiaodiSec day019 Learn Note 小迪安全学习笔记 记录得比较凌乱&#xff0c;不尽详细 C#相关 .NET 框架&#xff0c;多用 C#开发 内容非常少&#xff0c;和通用安全漏洞差不多 未授权访问 目录结构 反编译获得源码&#xff0c;dll 反编译 web.config 目录 dll 文件类似于…

Java -- (part15)

一.数学相关类 Math 1.概述:数学工具类 2.特点 a.构造私有 b.方法静态 3.使用:类名直接调用 4.方法 static int abs(int a)->求参数的绝对值 static double ceil(double a)->向上取整 static double floor(double a)->向下取整 static long round(double a…

设计模式- 享元模式(Flyweight Pattern)结构|原理|优缺点|场景|示例

设计模式&#xff08;分类&#xff09; 设计模式&#xff08;六大原则&#xff09; 创建型&#xff08;5种&#xff09; 工厂方法 抽象工厂模式 单例模式 建造者模式 原型模式 结构型&#xff08;7种&#xff09; 适配器…