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秋招笔试,人麻了#