马蜂窝编程题
// 第一题反正过了,可能不是最好的。。 public static void main2(String[] args) { Scanner sc = new Scanner(System.in); String in = sc.nextLine(); String[] items = in.split(" "); TreeMap<String, HashSet<String> > map = new TreeMap<>(); for (int i = 0; i < items.length; i++) { String[] item = items[i].split("-"); String id = item[0]; String city = item[1]; if(!map.containsKey(city)){ HashSet<String> set = new HashSet<>(); set.add(id); map.put(city, set); }else{ HashSet<String> set = map.get(city); set.add(id); map.put(city, set); } } for (int i = 0; i < 3; i++) { if(!map.isEmpty()) { String k1 = ""; int count1 = 0; for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) { if (entry.getValue().size() > count1) { count1 = entry.getValue().size(); k1 = entry.getKey(); } else if (entry.getValue().size() == count1) { if (k1.charAt(0) > entry.getKey().charAt(0)) { count1 = entry.getValue().size(); k1 = entry.getKey(); } } } System.out.println(k1 + " " + count1); map.remove(k1); } } }
第二题输入其实是按层遍历。。
剑指offer上的题
#笔试题目##马蜂窝#