1002:方便记忆的电话号码

这道题模拟即可,几点注意:

  1. 使用Map用TreeMap, 自带排序功能以减少使用Heap
  2. 补齐0不要用String.format太慢
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    static int[] table = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
    static String[] leadingZero= {"000000", "00000", "0000", "000", "00", "0", ""};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        scanner.nextLine();

        TreeMap<Integer, Integer> map = new TreeMap<>();

        for (int i = 0; i < N; ++i) {
            String raw = scanner.nextLine();
            int val = getNumber(raw);
            if (map.containsKey(val)) {
                map.replace(val, map.get(val) + 1);
            } else {
                map.put(val, 1);
            }
        }

        boolean flag = false;
        for (Integer x : map.keySet()) {
            int y = map.get(x);
            if (y > 1) {
                flag = true;
                String s = String.valueOf(x);
                s = leadingZero[s.length() - 1] + s;
                System.out.println(s.substring(0, 3) + '-' + s.substring(3, 7) + " " + y);
            }
        }

        if (!flag) System.out.println("No duplicates.");
    }

    static int getNumber (String raw) {
        int res = 0;
        for (int i = 0; i < raw.length(); ++i) {
            char ch = raw.charAt(i);
            if (ch == '-') continue;
            if (Character.isDigit(ch)) {
                res = res * 10 + (ch - '0');
            } else {
                res = res * 10 + table[ch - 'A'];
            }
        }
        return res;
    }
}

以下是错误示范
TLE

import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    static char[] table = {'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9','0'};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        scanner.nextLine();
        HashMap<String, Integer> map = new HashMap<>();
        PriorityQueue<String> queue = new PriorityQueue<>();

        for (int i = 0; i < N; ++i) {
            String s = getNumber(scanner.nextLine());
            if (map.containsKey(s)) {
                map.replace(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
                queue.add(s);
            }
        }

        if (queue.size() == N) {
            System.out.println("No duplicates.");
        } else {
            while (!queue.isEmpty()) {
                String s = queue.poll();
                int k = map.get(s);
                if (k >= 2)
                    System.out.println(s + " " + map.get(s));
            }
        }
    }
    private static String getNumber(String raw) {

        char[] seq = new char[8];

        int t = 0;

        for (int i = 0; i < raw.length(); ++i) {

            char ch = raw.charAt(i);

            if (ch == '-') continue;

            if (Character.isLetter(ch)) {
                seq[t++] = table[ch - 'A'];
            } else {
                seq[t++] = ch;
            }

            if (t == 3) seq[t++] = '-';
        }

        return String.valueOf(seq);
    }
}

Runtime Error (The array might be too large)

import java.util.Scanner;

public class Main {
    static int[] table = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
    static int[] count = new int[7777779];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < N; ++i) {
            int x = 0;
            String raw = scanner.nextLine();
            for (int j = 0; j < raw.length(); ++j) {
                char ch = raw.charAt(j);
                if (ch == '-') continue;
                if (Character.isLetter(ch)) {
                    x = x * 10 + table[ch - 'A'];
                } else {
                    x = x * 10 + (ch - '0');
                }
            }
            ++count[x - 2222222];
        }

        boolean flag = false;
        for (int i = 0; i < count.length; ++i) {
            if (count[i] >= 2) {
                int pos = i + 2222222;
                StringBuffer buffer = new StringBuffer(String.valueOf(pos));
                buffer.insert(3, "-");
                System.out.println(buffer.toString() + " " + count[i]);
                flag = true;
            }
        }
        if (!flag) {
            System.out.println("No duplicates.");
        }
    }

    static String getFormatNumber (int pos) {
        pos += 2222222;
        StringBuffer buffer = new StringBuffer(String.valueOf(pos));
        buffer.insert(3, "-");
        return buffer.toString();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 题目见:1002:方便记忆的电话号码 用Python3 写了一段程序: 输出截图如下: 一切很正常的样子,结果与题...
    江火独眠阅读 4,421评论 0 0
  • 01.{ 换行: Opening Brace Can't Be Placed on a Separate Lin...
    码农不器阅读 7,042评论 0 14
  • 我的老媽是個有故事的女同學,說起來三天三夜講不完,還能根據當時當刻的氛圍需要,分別剪輯為奉獻篇,立志篇,情懷篇,逆...
    此刻是金__阅读 1,396评论 1 1
  • 监听函数 选择监听参数 回调函数 可以利用进出的runloop的时候 计算一下runloop的执行时间 等等,希...
    JackCoding阅读 3,532评论 0 1
  • 友谊的小船说翻就翻 师徒的小船说翻就翻 酒坛子说翻就翻 醋坛子说翻就翻 有种艺术叫变脸 有种天气叫变天 有种颜色叫...
    武兵阅读 1,679评论 0 2

友情链接更多精彩内容