興業(yè)銀行筆試題 -- 文本讀取單詞并按出現(xiàn)次數(shù)和字典序排序輸出

先說個(gè)重點(diǎn):興業(yè)銀行筆試給了我python2.7和3.9的環(huán)境做題轨香,而我簡(jiǎn)歷明明寫的java...投的也是java相關(guān)的崗位...無(wú)語(yǔ)至極玩般。我在python的環(huán)境里寫了一會(huì)java代碼,寫不下去了挡鞍,選擇放棄起暮。這里復(fù)盤一下,這在IDEA環(huán)境里寫的朝刊。

  • 筆試題:給定一個(gè)文本,里面包含字符蜈缤,數(shù)字拾氓,空格,標(biāo)點(diǎn)符號(hào)底哥,小數(shù)點(diǎn)咙鞍。單詞: 以空格為間隔的字符串為單詞,不包括單詞頭尾的引號(hào)和小數(shù)點(diǎn)趾徽。數(shù)字與字母組成的也是單詞续滋,比如1st,3rd孵奶。
  • 輸出:將單詞出現(xiàn)的次數(shù)按從多到少輸出疲酌,輸出格式為:word,count。若次數(shù)相同了袁,按字典序的正序輸出(a朗恳,b,c...)载绿。

這里給出我的思路和解法

我的第一反應(yīng)粥诫,HashMap+雙向鏈表+自定義節(jié)點(diǎn)。map存儲(chǔ)word和count崭庸,雙向鏈表維護(hù)自定義節(jié)點(diǎn)Node怀浆,按次數(shù)和字典序排列。這里我沒用API怕享,而是自定義節(jié)點(diǎn)Node和頭尾節(jié)點(diǎn)执赡。

Node節(jié)點(diǎn)

    /** 內(nèi)部類,定義了每個(gè)節(jié)點(diǎn) */
    static class Node {
        String val;
        Node pre;
        Node next;
        int count;

        public Node(String val) {
            this.val = val;
            this.count = 1;
        }
    }
  1. 將讀取到的當(dāng)前行字符串轉(zhuǎn)換成單詞,同時(shí)統(tǒng)計(jì)次數(shù)熬粗,放入map中搀玖。這里還沒有構(gòu)建雙向鏈表余境,因?yàn)槿绻婚_始就構(gòu)建鏈表驻呐,那單詞的count每次改變灌诅,都要調(diào)整位置。
    /** 將當(dāng)前行字符串轉(zhuǎn)化成單詞,并納入map */
    public void convertStringToNode(String s) {
        int length = s.length();
        int ind = 0, left = 0;
        while (ind < length) {
            char c = s.charAt(ind);
            if (isNumOrLetter(c)) {
                ind++;
            } else {
                // skip: . " ' '; 如果此時(shí)前面一個(gè)是數(shù)字或字母,說明[left,ind)是單詞
                if (isNumOrLetter(s.charAt(ind - 1))) {
                    // find next letter
                    String sTmp = s.substring(left, ind);
                    // put it into map
                    if (!map.containsKey(sTmp)) {
                        // 不包含該sTmp,創(chuàng)建一個(gè)并插入
                        map.put(sTmp, new Node(sTmp));
                    } else {
                        // 否則計(jì)數(shù)+1
                        map.get(sTmp).count++;
                    }
                    left = ++ind;
                } else {
                    // 若前面一個(gè)不是數(shù)字字母,說明遇到了連續(xù)空格,或者空格+引號(hào)等情況
                    left = ++ind;
                }
            }
        }
    }
  1. map添加結(jié)束后含末,就需要構(gòu)建雙向鏈表了猜拾,構(gòu)建insert方法。compareString來比較次數(shù)和字典序佣盒。
    /** 比較兩個(gè)不同字符串大小,s1大則返回1,s2大返回-1 */
    private int compareString(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        // 都轉(zhuǎn)成小寫再比較
        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();

        int ind = 0;
        while (ind < len1 && ind < len2) {
            // 字符對(duì)應(yīng)的ASCII越大,說明越靠后,在字典序中反而越小
            if (s1.charAt(ind) > s2.charAt(ind)) {
                return -1;
            } else if (s1.charAt(ind) < s2.charAt(ind)) {
                return 1;
            } else {
                ind++;
            }
        }
        if (len1 == len2) {
            return 0; // 應(yīng)該用不到,因?yàn)楸容^的是兩個(gè)不相同的字符串
        }
        return ind == len1 ? -1 : 1;
    }

    /** 向Node鏈表中插入一個(gè)Node */
    public void insertNodeIntoList(Node node) {
        if (head == null) {
            head = tail = node;
        } else {
            // head和tail非空
            Node index = tail;
            // 先按出現(xiàn)次數(shù)排序
            while (index != null && node.count > index.count) {
                index = index.pre;
            }
            // count相同比較字符串
            while (index != null && index.count == node.count && compareString(node.val, index.val) > 0) {
                index = index.pre;
            }
            /* 插在index下面 */
            if (index == null) {
                // 插在開頭
                node.next = head;
                head.pre = node;
                head = node;
            } else if (index == tail) {
                // 插在結(jié)尾
                index.next = node;
                node.pre = index;
                tail = node;
            } else {
                // 插在中間
                node.next = index.next;
                index.next.pre = node;
                index.next = node;
                node.pre = index;
            }
        }
    }

    /** 調(diào)用 insertNodeIntoList 方法 */
    public void callInsertNode() {
        for (Map.Entry<String, Node> ele : map.entrySet()) {
            insertNodeIntoList(ele.getValue());
        }
    }

完整代碼如下:

    Node head;  // head節(jié)點(diǎn)最靠前
    Node tail;  // tail節(jié)點(diǎn)最靠后
    Map<String, Node> map = new HashMap<>();

    /** 判斷字符是不是數(shù)字或者字母 */
    private boolean isNumOrLetter(char c) {
        return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9');
    }

    /** 比較兩個(gè)不同字符串大小,s1大則返回1,s2大返回-1 */
    private int compareString(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        // 都轉(zhuǎn)成小寫再比較
        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();

        int ind = 0;
        while (ind < len1 && ind < len2) {
            // 字符對(duì)應(yīng)的ASCII越大,說明越靠后,在字典序中反而越小
            if (s1.charAt(ind) > s2.charAt(ind)) {
                return -1;
            } else if (s1.charAt(ind) < s2.charAt(ind)) {
                return 1;
            } else {
                ind++;
            }
        }
        if (len1 == len2) {
            return 0; // 應(yīng)該用不到,因?yàn)楸容^的是兩個(gè)不相同的字符串
        }
        return ind == len1 ? -1 : 1;
    }

    /** 調(diào)用 insertNodeIntoList 方法 */
    public void callInsertNode() {
        for (Map.Entry<String, Node> ele : map.entrySet()) {
            insertNodeIntoList(ele.getValue());
        }
    }

    /** 向Node鏈表中插入一個(gè)Node */
    public void insertNodeIntoList(Node node) {
        if (head == null) {
            head = tail = node;
        } else {
            // head和tail非空
            Node index = tail;
            // 先按出現(xiàn)次數(shù)排序
            while (index != null && node.count > index.count) {
                index = index.pre;
            }
            // count相同比較字符串
            while (index != null && index.count == node.count && compareString(node.val, index.val) > 0) {
                index = index.pre;
            }
            /* 插在index下面 */
            if (index == null) {
                // 插在開頭
                node.next = head;
                head.pre = node;
                head = node;
            } else if (index == tail) {
                // 插在結(jié)尾
                index.next = node;
                node.pre = index;
                tail = node;
            } else {
                // 插在中間
                node.next = index.next;
                index.next.pre = node;
                index.next = node;
                node.pre = index;
            }
        }
    }

    /** 將當(dāng)前行字符串轉(zhuǎn)化成單詞,并納入map */
    public void convertStringToNode(String s) {
        int length = s.length();
        int ind = 0, left = 0;
        while (ind < length) {
            char c = s.charAt(ind);
            if (isNumOrLetter(c)) {
                ind++;
            } else {
                // skip: . " ' '; 如果此時(shí)前面一個(gè)是數(shù)字或字母,說明[left,ind)是單詞
                if (isNumOrLetter(s.charAt(ind - 1))) {
                    // find next letter
                    String sTmp = s.substring(left, ind);
                    // put it into map
                    if (!map.containsKey(sTmp)) {
                        // 不包含該sTmp,創(chuàng)建一個(gè)并插入
                        map.put(sTmp, new Node(sTmp));
                    } else {
                        // 否則計(jì)數(shù)+1
                        map.get(sTmp).count++;
                    }
                    left = ++ind;
                } else {
                    // 若前面一個(gè)不是數(shù)字字母,說明遇到了連續(xù)空格,或者空格+引號(hào)等情況
                    left = ++ind;
                }
            }
        }
    }
    
    /** 內(nèi)部類,定義了每個(gè)節(jié)點(diǎn) */
    static class Node {
        String val;
        Node pre;
        Node next;
        int count;

        public Node(String val) {
            this.val = val;
            this.count = 1;
        }
    }


    public static void main(String[] args) {
        Main main = new Main();
        try {
            File file = new File("D:/Users/JackTheRipper/Desktop/test.txt");
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
            BufferedReader buffReader = new BufferedReader(reader);

            String strTmp;
            while ((strTmp = buffReader.readLine()) != null) {
                System.out.println(strTmp);
                // 將該行轉(zhuǎn)成單詞并納入map
                main.convertStringToNode(strTmp);
            }
            buffReader.close();
            // 將map的內(nèi)容納入鏈表
            main.callInsertNode();

        } catch (IOException e) {
            e.printStackTrace();
        }

        // 輸出結(jié)果
        Node cur = main.head;
        while (cur != null) {
            System.out.println(cur.val + "," + cur.count);
            cur = cur.next;
        }
    }

輸出結(jié)果:

I am "Derrick Rose", Nicknamed "Wind City Rose".
I like basketball, I am very strong.
I do not like singing and rap, I am very week.
Forever Bull No1
I,5
am,3
like,2
Rose,2
very,2
and,1
basketball,1
Bull,1
City,1
Derrick,1
do,1
Forever,1
Nicknamed,1
not,1
rap,1
singing,1
strong,1
week,1
Wind,1
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末挎袜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子肥惭,更是在濱河造成了極大的恐慌盯仪,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蜜葱,死亡現(xiàn)場(chǎng)離奇詭異全景,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)牵囤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門爸黄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人揭鳞,你說我怎么就攤上這事炕贵。” “怎么了野崇?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵称开,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我乓梨,道長(zhǎng)钥弯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任督禽,我火速辦了婚禮脆霎,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘狈惫。我一直安慰自己睛蛛,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布胧谈。 她就那樣靜靜地躺著忆肾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪菱肖。 梳的紋絲不亂的頭發(fā)上客冈,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音稳强,去河邊找鬼场仲。 笑死和悦,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的渠缕。 我是一名探鬼主播鸽素,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼亦鳞!你這毒婦竟也來了馍忽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤燕差,失蹤者是張志新(化名)和其女友劉穎遭笋,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體徒探,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坐梯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了刹帕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吵血。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖偷溺,靈堂內(nèi)的尸體忽然破棺而出蹋辅,到底是詐尸還是另有隱情,我是刑警寧澤挫掏,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布侦另,位于F島的核電站,受9級(jí)特大地震影響尉共,放射性物質(zhì)發(fā)生泄漏褒傅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一袄友、第九天 我趴在偏房一處隱蔽的房頂上張望殿托。 院中可真熱鬧,春花似錦剧蚣、人聲如沸支竹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)礼搁。三九已至,卻和暖如春目尖,著一層夾襖步出監(jiān)牢的瞬間馒吴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留饮戳,地道東北人豪治。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像莹捡,于是被迫代替她去往敵國(guó)和親鬼吵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子扣甲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容