马蜂窝编程题

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

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

相关推荐

03-18 01:22
门头沟学院 Java
多多爱我我爱多多:linkedList 替换 arrayList 是怎么实现20倍提升的 好奇
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
正在热议
更多
# 一张图晒出你司的标语 #
4237次浏览 75人参与
# AI面会问哪些问题? #
27346次浏览 548人参与
# 米连集团26产品管培生项目 #
13270次浏览 285人参与
# 你的实习产出是真实的还是包装的? #
19973次浏览 342人参与
# 找AI工作可以去哪些公司? #
8806次浏览 228人参与
# 春招至今,你的战绩如何? #
64037次浏览 575人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
14970次浏览 220人参与
# 从事AI岗需要掌握哪些技术栈? #
8694次浏览 296人参与
# 你做过最难的笔试是哪家公司 #
32770次浏览 226人参与
# 中国电信笔试 #
31627次浏览 285人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
340658次浏览 2173人参与
# 阿里笔试 #
178225次浏览 1311人参与
# 第一份工作一定要去大厂吗 #
14318次浏览 122人参与
# 金三银四,你的春招进行到哪个阶段了? #
22006次浏览 280人参与
# 沪漂/北漂你觉得哪个更苦? #
9702次浏览 193人参与
# HR最不可信的一句话是__ #
6124次浏览 113人参与
# 应届生第一份工资要多少合适 #
20660次浏览 86人参与
# AI时代,哪个岗位还有“活路” #
11369次浏览 339人参与
# 春招你拿到offer了吗 #
830972次浏览 9986人参与
# 长得好看会提高面试通过率吗? #
22430次浏览 254人参与
# 聊聊你的职场新体验 #
336397次浏览 1894人参与
# 学历对求职的影响 #
665009次浏览 4249人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务