Zoom 8.10 笔试第二题股票推荐 当时没做出来

import java.util.*;

public class Main{
    static Map<String, Set<String>> map = new HashMap<>();//股民姓名为Key,Set集合为订阅股票 Value
    static Set<String> resultSet = new LinkedHashSet<>();//推荐的股票集合
    static List<String> nameList = new LinkedList<>();//股民姓名链表 方便遍历

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = Integer.parseInt(scanner.nextLine());//输入操作次数

        while(count>0) {
            count--;
            String stepStr = scanner.nextLine();
            String[] steps = stepStr.split(" ");//每一步操作的 steps 例如 1 Alice 2  注意此处不包含插入的订阅股票neirong内容
            if(steps[0].equals("1")|| steps[0].equals("2")){
                //插入或者查询操作
                if(steps[0].equals("1")){

                    //插入
                    Set<String> set = new LinkedHashSet<>();
                    //读取插入
                    String insertStr = scanner.nextLine();
                    String[] insertContent = insertStr.split(" ");                     
                    for (String content : insertContent) {//加入集合
                        set.add(content);
                    }
                    //加数据 加名字
                    map.put(steps[1],set);
                    nameList.add(steps[1]);
                } else if(steps[0].equals("2")){
                    //查找
                    if(!map.containsKey(steps[1])){//不存在输出error
                        System.out.println("error");
                        continue;
                    }else {
                        //返回个数
                        int searchNum = search(steps[1]);
                        System.out.println(searchNum);
                    }
                }
            }

        }


    }
    public static int search(String name){

        Set<String> curSet = map.get(name);

        for (String otherName : nameList) {
            if(!name.equals(otherName)){//遍历查询与当前的用户 订阅有交集的股民
                Set<String> otherSet = new LinkedHashSet<>(map.get(otherName));//切记new
                Set<String> otherNoDelSet = new LinkedHashSet<>(map.get(otherName));//切记new

                otherSet.retainAll(curSet);
                if(otherSet.size()==0){//0说明无交集 无法推荐
                    continue;
                }else {
                    //删除交集加入到总结果集合
                    otherNoDelSet.removeAll(otherSet);
                    resultSet.addAll(otherNoDelSet);
                }

            }

        }
        int nums = resultSet.size();
        resultSet.clear();
        return nums;


    }    

#zoom校招##做完zoom2023秋招笔试,人麻了#
全部评论
你这个每次都要遍历一下,如果股票总数量级为M,人的数量级为N的话,每次去和其他人取交集,复杂度已经是O(M*N)了,每个人都这样做一下,复杂度为O(M*N^2)了,你这时间能过得去么
点赞 回复 分享
发布于 2022-08-10 22:47
可以看看你的第一题么,看他们C++好短
点赞 回复 分享
发布于 2022-08-10 21:24

相关推荐

不愿透露姓名的神秘牛友
06-27 20:15
点赞 评论 收藏
分享
叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务