1002_Java

Input
輸入的格式是,第一行是一個正整數(shù),指定電話號碼薄中號碼的數(shù)量(最多100000)。余下的每行是一個電話號碼。每個電話號碼由數(shù)字,大寫字母(除了Q和Z)以及連接符組成。每個電話號碼中只會剛好有7個數(shù)字或者字母。

Output
對于每個出現(xiàn)重復(fù)的號碼產(chǎn)生一行輸出,輸出是號碼的標(biāo)準(zhǔn)格式緊跟一個空格然后是它的重復(fù)次數(shù)。如果存在多個重復(fù)的號碼,則按照號碼的字典升序輸出。如果輸入數(shù)據(jù)中沒有重復(fù)的號碼,輸出一行: "No duplicates. "

解題思路:
1、不需要判斷輸入內(nèi)容的格式(系統(tǒng)測試算法時使用的數(shù)據(jù)都是符合輸入格式的數(shù)據(jù))
2、輸入的內(nèi)容先進(jìn)行格式化(字母替換成數(shù)字,去掉輸入的橫線“-”,再在index=3處插入橫線“-”)
3、將格式化后的內(nèi)容插入到一個TreeMap(TreeMap 默認(rèn)排序規(guī)則:按照key的字典順序來排序(升序))中
4、遍歷輸出TreeMap元素

難點:
在多次提交中一直提示“Time Limit Exceeded”,排查原因是在循環(huán)中使用了String的replace方法,該方法比較耗時,在數(shù)據(jù)量大時表現(xiàn)特別明顯(這道題目,系統(tǒng)測試算法時,應(yīng)該使用的上萬條數(shù)據(jù))。

難點解決方案:
使用StringBuilder替換replace

import java.util.*;

public class Main {
    private static HashMap<Character, Character> dict = new HashMap<Character, Character>() {
        {
            put('A', '2');
            put('B', '2');
            put('C', '2');
            put('D', '3');
            put('E', '3');
            put('F', '3');
            put('G', '4');
            put('H', '4');
            put('I', '4');
            put('J', '5');
            put('K', '5');
            put('L', '5');
            put('M', '6');
            put('N', '6');
            put('O', '6');
            put('P', '7');
            put('R', '7');
            put('S', '7');
            put('T', '8');
            put('U', '8');
            put('V', '8');
            put('W', '9');
            put('X', '9');
            put('Y', '9');
        }
    };

    public static void main(String args[]) {
        TreeMap<String, Integer> resultMap = new TreeMap<String, Integer>();
        Scanner cin = new Scanner(System.in);
        int count = cin.nextInt();
        for (int i = 0; i < count; i++) {
            String next = formatInput(cin.next());
            if (!resultMap.containsKey(next)) {
//                map中沒有該元素時
                resultMap.put(next, 1);
            } else {
//               map中已經(jīng)有了該元素
                resultMap.put(next, resultMap.get(next) + 1);
            }
        }
        boolean repeat = false;
        for (Map.Entry<String, Integer> stringIntegerEntry : resultMap.entrySet()) {
            if (stringIntegerEntry.getValue() > 1) {
                System.out.println(stringIntegerEntry.getKey() + " " + stringIntegerEntry.getValue());
                if (!repeat) {
                    repeat = true;
                }
            }
        }
        if (!repeat) {
            System.out.println("No duplicates.");
        }
    }


    private static String formatInput(String inputNumber) {
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < inputNumber.length(); i++) {
            char charAt = inputNumber.charAt(i);
            if (dict.containsKey(charAt)) {
                temp.append(dict.get(charAt));
            } else if (charAt == '-') {
//                do nothing
            } else {
                temp.append(charAt);
            }
        }
        temp.insert(3, "-");
        return temp.toString();
    }
}

Sample Input

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279

Sample Output

310-1010 2
487-3279 4
888-4567 3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容