美文网首页
1002:方便记忆的电话号码

1002:方便记忆的电话号码

作者: Lairai | 来源:发表于2018-05-05 15:38 被阅读0次

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

    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();
        }
    }
    

    相关文章

      网友评论

          本文标题:1002:方便记忆的电话号码

          本文链接:https://www.haomeiwen.com/subject/ewqlrftx.html