1. 给你一个chatting log file,format大概是这样的:
A: bla
B: bla bla
C: bla bla bla
要你找出说话最多(看word number) 的K个人
而且代码要从读file开始写
/*
1. 给你一个chatting log file,format大概是这样的:
A: bla
B: bla bla
C: bla bla bla
要你找出说话最多(看word number) 的K个人
而且代码要从读file开始写 */
public class Main {
public static void main(String[] args) {
List<String> lines =new ArrayList<>();
lines.add("A: bla");
lines.add("A: bla");
lines.add("B: bla bla");
lines.add("C: bla bla bla");
lines.add("A: bla"); List<String> list = new Solution().findMostPeopleTest(lines);
for(String name : list){
System.out.println(name);
}
}
} class Solution{
/*
public List<String> findMostPeople (String filePath) throws Exception {
HashMap<String, Integer> map = new HashMap<>();
int maxCounts = 0;
List<String> list = new ArrayList<>();
//read from file
File file = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(filePath));
String st;
while((st = br.readLine()) != null){
//can be replaced by other string related functions
String[] strs = st.split(": ");
String name = strs[0];
String[] words =strs[1].split(" ");
int wordsCount = words.length;
int totalCounts = map.getOrDefault(name, 0)+wordsCount;
map.put(name, totalCounts);
if(totalCounts > maxCounts){
list.clear();
list.add(name);
}
else if (totalCounts == maxCounts){
list.add(name);
}
}
return list;
}
*/
public List<String> findMostPeopleTest (List<String> lines) {
HashMap<String, Integer> map = new HashMap<>();
int maxCounts = 0;
List<String> list = new ArrayList<>();
//read from file
//File file = new File(filePath);
//BufferedReader br = new BufferedReader(new FileReader(filePath));
for(String st : lines){
//can be replaced by other string related functions
String[] strs = st.split(": ");
String name = strs[0];
String[] words =strs[1].split(" ");
int wordsCount = words.length; int totalCounts = map.getOrDefault(name, 0)+wordsCount;
System.out.println(name + " : "+totalCounts);
map.put(name, totalCounts);
if(totalCounts > maxCounts){
list.clear();
list.add(name);
maxCounts = totalCounts;
}
else if (totalCounts == maxCounts){
list.add(name);
}
}
return list;
} }

Google - Find Most People in Chat Log的更多相关文章

  1. Google glog 使用

    Google glog 使用 1        简介 Googleglog 库实现了应用级的日志记录,提供了C++ 风格的流操作和各种助手宏. 代码示例: #include <glog/logg ...

  2. Python + logging 输出到屏幕,将log日志写入文件

    日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事件.事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数 ...

  3. 百度Apollo解析——2.log系统

    Apollo中的glog 在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善. 1. 安装 以下是官方 ...

  4. Python + logging输出到屏幕,将log日志写入到文件

    logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...

  5. c++ google glog模块安装和基本使用(ubuntu)环境

    1,如何安装 1 Git clone https://github.com/google/glog.git 2 cd glog 3 ./autogen.sh 4 ./configure --prefi ...

  6. Blazor组件自做五 : 使用JS隔离封装Google地图

    Blazor组件自做五: 使用JS隔离封装Google地图 运行截图 演示地址 正式开始 1. 谷歌地图API 谷歌开发文档 开始学习 Maps JavaScript API 的最简单方法是查看一个简 ...

  7. VBV Rate Control

    Part 1 <06/05/07 12:08pm> Manao | he is negating a float by printing it, adding a "-" ...

  8. python自动化运维之路~DAY5

    python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...

  9. How to calculate bits per character of a string? (bpc) to read

      http://stackoverflow.com/questions/17797922/how-to-calculate-bits-per-character-of-a-string-bpc up ...

随机推荐

  1. flex外包团队—北京动点软件:推荐一本不错的Flex书籍

    内容介绍:Ready to put your ActionScript 3 skills to work on mobile apps? This hands-on book walks you th ...

  2. 20175317 《Java程序设计》第四周学习总结

    20175317 <Java程序设计>第四周学习总结 教材学习内容总结 第四周我学习了教材第五章的内容,了解了子类与继承的知识,学到了以下内容: 明白了什么是子类与父类.类的树形结构. 子 ...

  3. [JavaScript] 设置函数同名变量为false会导致函数无法执行

    var findEmail=false; function findEmail(){ alert("findEmail");} 这样函数不会运行. 为了保证函数可以运行,修改为: ...

  4. Police Stations CodeForces - 796D (bfs)

    大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...

  5. Ubuntu环境下,项目出现:Call to undefined function curl_init() 提示

    原因: 没有开启curl扩展 安装或者开启扩展 ubuntu 执行安装Curl的扩展 sudo apt-get install -y php-curl 同: mysql,redis,curl,等. 摘 ...

  6. 『PyTorch』第五弹_深入理解Tensor对象_下:从内存看Tensor

    Tensor存储结构如下, 如图所示,实际上很可能多个信息区对应于同一个存储区,也就是上一节我们说到的,初始化或者普通索引时经常会有这种情况. 一.几种共享内存的情况 view a = t.arang ...

  7. js实现下雪雪花特效

    两种下雪特效: 1. <script type="text/javascript"> (function($){ $.fn.snow = function(option ...

  8. AVL平衡二叉树实现

    #include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define EH 0 #define ...

  9. linux-Centos7安装python3并与python2共存

    1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...

  10. Linux 服务器中木马及木马清除

    1.查看流量图发现问题 查看的时候网页非常卡,有的时候甚至没有响应 2.top动态查看进程 我马上远程登录出问题的服务器,远程操作很卡,网卡出去的流量非常大,通过top发现了一个异常的进程占用资源比较 ...