码迷,mamicode.com
首页 > 其他好文 > 详细

Google - Find Most People in Chat Log

时间:2018-11-24 11:28:55      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:tor   null   ota   row   nes   for   integer   related   The   

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

标签:tor   null   ota   row   nes   for   integer   related   The   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!