马蜂窝编程题

// 第一题反正过了,可能不是最好的。。

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上的题
#笔试题目##马蜂窝#
全部评论
第二题输入其实是按层遍历。。 剑指offer上的题  public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         String in = sc.nextLine();         String target = sc.nextLine();         String[] items = in.split(",");         System.out.println(Arrays.toString(items));         int index = 0;         for (int i = 1; i <= items.length; i++) {             if(items[i-1].equals(target))                 index = i;         }         if(index == 0) {System.out.println(-1);return;}         // 有右子树         if(index * 2 + 1 <= items.length){             // 找右子树的最左             int left = index * 2 + 1;             while(left * 2 <= items.length){                 left *= 2;             }             System.out.println(left);             return;         }else{ //梅子树 在判断             if(index == 1){                 System.out.println(-1);return;             }             while(index != 1) {                 int p = findP(index);                 if (isLeft(index)) { //当前是父节点的左                     System.out.println(p);                     return;                 } else { // 当前是父节点的right                     index = p;                 }             }             System.out.println(-1);         }     }     public static int findP(int index){         return index / 2;     }     public static boolean isLeft(int index){         if((index & 1) == 0)             return true;         return false;     }
点赞 回复
分享
发布于 2019-09-23 20:34

相关推荐

1 1 评论
分享
牛客网
牛客企业服务