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. leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...

  2. 7、zabbix使用进阶(03)

    节知识点: zabbix自动发现 web监控 zabbix自动发现   官网:https://www.zabbix.com/documentation/4.0/zh/manual/discovery/ ...

  3. python requests用法总结

    requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到.可以说,Requests 完全满足如今网络的需求 本文全部来源于官方文档 http://docs ...

  4. P1108 低价购买

    传送门 思路: 对于第一问很容易看出是求最长下降子序列,N2 的暴力就可解决.而第二问是求最优方案数(且不重复),需要判重.可以在求解最长下降子序列的基础上增开一个数组 g ,g[ i ] 表示以 i ...

  5. Font Awesome字体图标的 用法, 很简单

    http://fontawesome.dashgame.com/ 上面是 官网,  可下载,也可以CDN. 1...  加载 2...  用法

  6. 在input中输入需要的数据,使用qrcode,点击生成二维码

    话不多说直接上代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...

  7. BFS GPLT L2-016 愿天下有情人都是失散多年的兄妹

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 分析:一开始以为是并查集..... ...

  8. 『OpenCV3』滤波器实现及使用滤波器降噪

    一.滤波器实现 我们实现这样一个基于拉普拉斯算子的滤波器核心,并使用它进行滤波,这可以做到锐化图像的效果, 0 -1 0 -1 5 -1 0 -1 0 首先我们完全手动的进行滤波,依赖指针操作, vo ...

  9. zend cache使用

    require_once 'Zend/Cache.php';//引用文件$frontendOptions = array( 'lifeTime' => 60, // cache lifetime ...

  10. YII实现restful,postman进行接口测试

    Yii2 restful API文档 一.配置模块: 1.Config/main.php: 2.创建模块目录: 3.Module.php: 二.路由配置: 三.控制器: 四.Models: 五.测试: ...