首页 > 其他 > 详细

Google - Find Most People in Chat Log

时间:2018-11-24 10:29:41      阅读:138      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/incrediblechangshuo/p/10010643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!