JAVA学习日记----------
来源:互联网 发布:骰子软件 编辑:程序博客网 时间:2024/06/04 18:11
JAVA学习日记----------
今天仅有TopCoder例题更新
Problem Statement
Most modern text editors are able to give some statistics about the text they are editing. One nice statistic is the average word length in the text.
A word is a maximal continuous sequence of letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces, digits, and punctuation marks.
The average word length is the sum of all the words' lengths divided by the total number of words. For example, in the text "This is div2 easy problem.", there are 5 words: "This", "is", "div", "easy", and "problem". The sum of the word lengths is 4+2+3+4+7=20, so the average word length is 20/5=4.
Given a String text, return the average word length in it. If there are no words in the text, return 0.0.
Definition
Class:TextStatisticsMethod:averageLengthParameters:StringReturns:doubleMethod signature:double averageLength(String text)(be sure your method is public)算法如下:
public class TextStatistics {
public double averageLength(String text) {
double count = 0;
int sum = 0;
String[] strList = text.split("[0-9 ,.?!-]+");
for (String str : strList) {
sum = sum + str.length();
}
if(strList.length!=0){
count = sum / strList.length;
return count;
}
return 0;
}
}
今天学习到的东西:
1. 上面算法可以将给出的字符串按照单词分割,举个例子:“This0 is1 a2 test3 text4?”通过算法后,变为“This”,“is”,“a”,“test”,"text", 但是算法有问题这个测试用例就是错误的“!This0 is1 a2 test3 text4?”
2.正则表达式"[0-9 ,.?!-]+"表示多次匹配数字0到9,空格,逗号,句号,问号,叹号,破折号。
P.S.另外请看到这篇Blog的同学能给出一个完善算法,我们共同交流共同进步~~~~~~~
- java学习日记(线程)
- java学习日记
- Java学习日记
- Java学习日记(一)
- JAVA学习日记
- Java高新技术学习日记
- java学习日记1
- Java学习日记
- java学习日记
- 学习日记-java高新技术
- Java 基础学习日记
- JAVA学习日记
- Java学习日记1
- java学习日记
- java学习日记
- java学习日记01
- java学习日记02
- Java 的学习日记
- chapter02
- crackMe的逆向分析
- 【Android学习】网络通信----以HttpURLConnection Post请求为例
- UVa Spreading the Wealth-11300(数学推导+中位数)
- AtomicReference(无锁的对象引用)与AtomicStampedReference与(带有时间戳的对象引用)
- JAVA学习日记----------
- 1008. 数组元素循环右移问题 (20) Python 2编译
- 黑白棋算法简单实现与基于Qt的GUI编程的综合应用
- Java基础教程35-LinkedList和ArrayListed的区别
- List删除元素报Exception in thread "main" java.util.ConcurrentModificationException异常,或数据删除不完整
- Kotlin学习(二)——基本命令行操作hello world
- 被低估的BIRT报表(二):Birt也可以很漂亮
- 从vue文件中抽取出子组件的流程及过程中踩过的坑
- Opencv绘制灰度/彩色图像的直方图及直方图的均衡化实例及源代码