Google - Find Most People in Chat Log
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的更多相关文章
- Google glog 使用
Google glog 使用 1 简介 Googleglog 库实现了应用级的日志记录,提供了C++ 风格的流操作和各种助手宏. 代码示例: #include <glog/logg ...
- Python + logging 输出到屏幕,将log日志写入文件
日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事件.事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数 ...
- 百度Apollo解析——2.log系统
Apollo中的glog 在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善. 1. 安装 以下是官方 ...
- Python + logging输出到屏幕,将log日志写入到文件
logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...
- c++ google glog模块安装和基本使用(ubuntu)环境
1,如何安装 1 Git clone https://github.com/google/glog.git 2 cd glog 3 ./autogen.sh 4 ./configure --prefi ...
- Blazor组件自做五 : 使用JS隔离封装Google地图
Blazor组件自做五: 使用JS隔离封装Google地图 运行截图 演示地址 正式开始 1. 谷歌地图API 谷歌开发文档 开始学习 Maps JavaScript API 的最简单方法是查看一个简 ...
- VBV Rate Control
Part 1 <06/05/07 12:08pm> Manao | he is negating a float by printing it, adding a "-" ...
- python自动化运维之路~DAY5
python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...
- 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 ...
随机推荐
- JavaScript(类型转换、条件语句、循环、函数)
类型装换 转为数字类型 // Number console.log(Number(undefined)); //NaN console.log(Number(null)); //0 console.l ...
- com.borland.jbcl.layout.*;(XYLayout)
因为某些原因,涉及到需要运行一个十几年前的项目,项目一直报错,缺少.layoutXY,找了好久,CSDN那里一直需要下载,而且收费,而且很麻烦,本来都放弃了的这个jar包原来是java的IDE工具JB ...
- 盛最多水的容器(java实现)
题目: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的 ...
- 《SQL 基础教程》第三章:聚合和排序
这一章节主要讲了三方面的内容: 数据的汇总操作a. 聚合函数b.分组操作 给汇总操作指定条件 对汇总结果进行排序 COUNT()等聚合函数 定义: 输入多行,输出一行的函数称为聚合函数 功能: 用于对 ...
- JWT ajax java spingmvc 简洁教程
1.添加依赖 <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</ ...
- 压力测试+接口测试(工具jmeter)
jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因 为jmeter是java开发的,所以运行的时候必须 ...
- 『Python CoolBook』C扩展库_其二_demo演示
点击进入项目 C函数源文件 /* sample.c */ #include "sample.h" /* Compute the greatest common divisor */ ...
- windows服务项目的 安装 卸载 查看
安装服务:installutil.exe C:\a.exe卸载服务Installutil.exe /u C:\a.exe 查看服务状态 services.msc
- bzoj3884 上帝的集合
根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α α ”.“α α 被定义为“元”构成的集合.容 ...
- Python3解析dex文件
一.说明 1.1 背景说明 看<加密与解密>的时候反复听说“PE文件格式”,到Android安全兴起就不断听说“dex文件格式”.意思是看得懂的,但自己不能手解析一番总觉得不踏实,所以决定 ...