天天看點

筆試面試題之wordcount(出自網易)

各大公司公開了的筆試面試題練習之網易遊戲2016“營運開發工程師”線上測評題wordcount解答

全部代碼下載下傳:

Github連結:https://github.com/wpeace1212/Coder/tree/master/Code/netease

寫文章不易,歡迎大家采我的文章,以及給出有用的評論,當然大家也可以關注一下我的github;多謝;

##題目1.wordcount 時間限制: 2000ms 單點時限: 1000ms 記憶體限制: 256MB

描述

有一篇文章隻包含英文單詞,請按它們的出現次數排序,出現最多的先輸出;如果出現次數相等的,按字元串ASCII排序升序輸出。

輸入

第一行會是行數N( 0 < N <= 500)。

之後N行中的每行會是一個或多個英文單詞。

資料總大小不超過100K。

輸出

輸出:

<單詞1><空格><次數1>

<單詞2><空格><次數2>

樣例輸入

4

SQL DW

SQL AS DW

AB

SQL      
樣例輸出
SQL 3

DW 2

AB 1

AS 1

      

2.我的了解:

  1. 單詞計數

用map計數是比價簡便也容易想到的不是難點

  1. 排序

關鍵是對建單詞和值次數進行排序,需要想到關鍵性的Entry獲得鍵值對,然後進行排序就簡單了。

3.我的代碼:

package com.netease.yunyin;



import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;



public class WordCount {

      public static void main(String[] args) {

        //獲得輸入

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        //将單詞和次數存成鍵值對

        Map<String, Integer>words=new HashMap<>();

        //行數

        int n=;



        try {

            //行數獲得

            n= Integer.parseInt(br.readLine());

        } catch (NumberFormatException e1) {

            e1.printStackTrace();

        } catch (IOException e1) {

            e1.printStackTrace();

        }

          //處理輸入:

          while(true){

               try {

                   //按行處理

                String lin=br.readLine();

                n--;

                //對每一行的單詞單獨處理

                String[] strings = lin.split("\\s");

                for(String k:strings){

                    //判斷計數

                    if(!"".equals(k)&&words.get(k)!=null){

                        int wordCount=words.get(k);

                        words.put(k,wordCount+);

                    }else if(k!=null&&!"".equals(k)){

                        words.put(k, );

                    }



                }

            } catch (IOException e) {

                e.printStackTrace();

            }

               //處理完所有行時,跳出

              if(n<)break;

          }

            //獲得Map的鍵值對并存到Entry

        List<Entry<String, Integer>> sortList=new ArrayList<>(words.entrySet());

           //進行排序,實作Comparator接口

        Collections.sort(sortList, new Comparator<Entry<String, Integer>>() {



            @Override

            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {

                //次數大的放前面

                if(o1.getValue()>o2.getValue())

                    return -;

                //次數相同的按,單詞排序

                else if(o1.getValue()==o2.getValue())

                    return o1.getKey().compareTo(o2.getKey());

                return ;

            }

        });

        //循環輸出:

        for(Entry<String, Integer> e:sortList){



            System.out.println(e.getKey()+" "+e.getValue());

        }

      }

}
           

4.我的測試結果:

筆試面試題之wordcount(出自網易)

好的本章介紹到這裡

來自伊豚wpeace(rlovep.com)