码迷,mamicode.com
首页 > 其他好文 > 详细

1002 重复电话号码

时间:2019-11-26 13:13:54      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:int   scanner   重复   length   har   inpu   rac   NPU   class   

runtime error 又不能查看。不知道什么报错,本地没问题

package oj;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int rows, i, j, phoneNumberCharLength, char2Number;
        String phoneNumberInput;
        StringBuilder phoneNumberFormat = new StringBuilder();
        char[] phoneNumberCharArray;
        Map<String, Integer> map = new HashMap<String, Integer>(16);
        Scanner scanner = new Scanner(System.in);
        rows = scanner.nextInt();
        scanner.nextLine();
        for (i = 0; i < rows; i++) {
            phoneNumberFormat.delete(0, phoneNumberFormat.length());
            phoneNumberInput = scanner.nextLine();
            phoneNumberCharArray = phoneNumberInput.toCharArray();
            phoneNumberCharLength = phoneNumberCharArray.length;
            for (j = 0; j < phoneNumberCharLength; j++) {
                char2Number = Character.getNumericValue(phoneNumberCharArray[j]);
                if (char2Number != -1) {
                    if (char2Number <= 9) {
                        phoneNumberFormat.append(phoneNumberCharArray[j]);
                        continue;
                    }
                }
                if (phoneNumberCharArray[j] == ‘-‘) {
                    continue;
                }
                switch (phoneNumberCharArray[j]) {
                    case ‘A‘:
                    case ‘B‘:
                    case ‘C‘:
                        phoneNumberFormat.append(2);
                        break;
                    case ‘D‘:
                    case ‘E‘:
                    case ‘F‘:
                        phoneNumberFormat.append(3);
                        break;
                    case ‘G‘:
                    case ‘H‘:
                    case ‘I‘:
                        phoneNumberFormat.append(4);
                        break;
                    case ‘J‘:
                    case ‘K‘:
                    case ‘L‘:
                        phoneNumberFormat.append(5);
                        break;
                    case ‘M‘:
                    case ‘N‘:
                    case ‘O‘:
                        phoneNumberFormat.append(6);
                        break;
                    case ‘P‘:
                    case ‘R‘:
                    case ‘S‘:
                        phoneNumberFormat.append(7);
                        break;
                    case ‘T‘:
                    case ‘U‘:
                    case ‘V‘:
                        phoneNumberFormat.append(8);
                        break;
                    case ‘W‘:
                    case ‘X‘:
                    case ‘Y‘:
                        phoneNumberFormat.append(9);
                        break;
                    default:
                }
            }
            //在第三位后插入小横杠"-"
            if (phoneNumberFormat.length() > 3) {
                phoneNumberFormat.insert(3, "-");
            }
            //对map进行判断,计数
            if (map.containsKey(phoneNumberFormat.toString())) {
                Integer count = map.get(phoneNumberFormat.toString());
                count++;
                map.put(phoneNumberFormat.toString(), count);
            } else {
                map.put(phoneNumberFormat.toString(), 1);
            }
        }
        //如果keySet都为1
        Set<String> keySet = map.keySet();
        Object[] keyArray = keySet.toArray();
        int length = keyArray.length;
        for (i = 0; i < length; i++) {
            if (map.get(keyArray[i]) == 1) {
                map.remove(keyArray[i]);
            }
        }
        //更新set值
        keySet = map.keySet();
        keyArray = keySet.toArray();
        length = keyArray.length;
        if (map.size() > 0) {
            Arrays.sort(keyArray);
            for (i = 0; i < length; i++) {
                System.out.println(keyArray[i] + " " + map.get(keyArray[i]));
            }
        } else {
            System.out.println("No duplicates.");
        }

    }
}

 

1002 重复电话号码

标签:int   scanner   重复   length   har   inpu   rac   NPU   class   

原文地址:https://www.cnblogs.com/yangzihong/p/11934632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!