网易互娱,第二题

网易互娱,第二题文件系统,有没有大佬分享一下代码啊。我这是该考虑的都考虑了,我想知道还有哪些坑。dup2可能产生新的key考虑了,open同一个文件输出同一个key考虑了。不晓得还该考虑啥
public class huyu02 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            Map<Integer, String> map = new HashMap<>();
            List<Integer> list = new LinkedList<>();
            int n = in.nextInt();
            in.nextLine();
            int max = 0;
            for (int j = 0; j < n; j++) {

                String s = in.nextLine();
                String[] temp = s.split("\\s+");
                if (temp[0].equals("open")) {

                    if (list.size() == 0) {
                        map.put(0, temp[1]);
                        list.add(0);
                        System.out.println(0);
                    } else {
                        if (map.containsValue(temp[1])) {
                            for (Integer key : map.keySet()) {
                                if (map.get(key).equals(temp[1])) {
                                    System.out.println(key);
                                    break;
                                }
                            }
                            continue;
                        }
                        max = Collections.max(list) + 1;
//                        System.out.println(max);
                        for (int k = 0; k <= max; k++) {
                            if (!list.contains(k)) {
                                map.put(k, temp[1]);
                                list.add(k);
                                System.out.println(k);
                                break;
                            }
                        }
                    }
                } else if (temp[0].equals("dup")) {
                    max = Collections.max(list) + 1;

                    for (int k = 0; k <= max; k++) {
                        if (!list.contains(k)) {
                            map.put(k, map.get(Integer.valueOf(temp[1])));
                            list.add(k);
                            System.out.println(k);
                            break;
                        }
                    }
                } else if (temp[0].equals("dup2")) {
                    map.put(Integer.valueOf(temp[2]), map.get(Integer.valueOf(temp[1])));
                    if (!list.contains(temp[2])) {
                        list.add(Integer.valueOf(temp[2]));
                    }

                } else if (temp[0].equals("close")) {
                    int value = Integer.valueOf(temp[1]);
                    map.remove(value);
                    for (int p = 0; p < list.size(); p++) {
                        if (list.get(p) == value) {
                            list.remove(p);
                            break;
                        }
                    }
                } else if (temp[0].equals("query")) {
                    System.out.println(map.get(Integer.valueOf(temp[1])));

                }
            }

        }
    }
}

#网易互娱##笔试题型#
全部评论
https://www.nowcoder.com/discuss/527950?toCommentIpt=1,看到了一个AC的
点赞 回复 分享
发布于 2020-09-27 22:07

相关推荐

牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
评论
点赞
1
分享
牛客网
牛客企业服务