Google - Find Most People in Chat Log

Posted incrediblechangshuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1598 find the most comfortable road

hdu1598 find the most comfortable road 枚举+最小生成树

JAVA 面试题: Find most frequency word in a paragraph

hdu1598 find the most comfortable road (枚举)+并查集

HDU 1598 find the most comfortable road(最小生成树)

使用 google people API 访问 google 连接