莉莉丝游戏笔试记录

1题 重排链表。
时间复杂度O(n),空间复杂度O(1)
class ListNode {
  int val;
  ListNode next = null;
  public ListNode(int val) {
    this.val = val;
  }
}


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    public ListNode formatList (ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode tail = head;
        ListNode p = head.next;
        ListNode next = null;
        while(p != null) {
            tail.next = p;
            tail = p;
            p = p.next;
            if (p == null) {
                break;
            }
            next = p.next;
            p.next = head;
            head = p;
            p = next;
        }
        tail.next = null;
        return head;
    }
}
2题 有约束的链表排序
时间复杂度O(n*logk) ,空间复杂O(n),其中n为链表长度,k为链表中不重复的数字个数。
import java.util.*;

class ListNode {
  int val;
  ListNode next = null;
  public ListNode(int val) {
    this.val = val;
  }
}


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    public ListNode sortList (ListNode head) {
        if (head == null) {
            return null;
        }
        TreeMap<Integer, LinkedList<ListNode>> sortedMap = new TreeMap<>();
        ListNode p = head;
        while(p != null) {
            List<ListNode> list = sortedMap.computeIfAbsent(p.val, k -> new LinkedList<>());
            list.add(p);
            p = p.next;
        }
        head = null;
        while(!sortedMap.isEmpty()) {
            Iterator<LinkedList<ListNode>> it = sortedMap.values().iterator();
            while (it.hasNext()) {
                LinkedList<ListNode> list = it.next();
                if (head == null) {
                    head = p = list.removeFirst();
                } else {
                    p.next = list.removeFirst();
                    p = p.next;
                }
                if (list.isEmpty()) {
                    it.remove();
                }
            }
        }
        if (p != null) {
            p.next = null;
        }
        return head;
    }
}
3题 消息队列。
时间复杂度O(n),空间复杂度O(n),其中获取队首消息、获取指定类型的消息、添加消息单次操作的时间复杂度都是O(1)。
消息队列设计思想:使用一个双端队列messageQueue存放消息,再使用一个哈希表将指定类型消息串联起来,键为消息类型,值为队列。
  • 添加消息:将消息添加到messageQueue,同时添加也添加到哈希表中的特定队列中。由于存的都是引用变量,所以每个消息并没有占两份内存。
  • 获取消息:
(1)当消息类型为0,也就是获取messgeQueue队首的消息时,将消息类型置为队首消息的类型;此时获队首消息转换成了获取特定类型的第一个消息。   
(1)获取指定类型的消息。从哈希表中取出消息体,然后维护好messageQueue双端队列。
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        StringBuilder buf = new StringBuilder();
        MessageQueue messageQueue = new MessageQueue();
        while(n-- > 0) {
            String opt = sc.next();
            if (opt.equals("in")) {
                messageQueue.put(sc.nextInt(), sc.next());
            } else if (opt.equals("out")) {
                buf.append(messageQueue.get(sc.nextInt())).append("\n");
            }
        }
        System.out.println(buf);
    }

}

class MessageQueue {

    HashMap<Integer, LinkedList<Entity>> hashMap = new HashMap<>();
    Entity head, tail;

    public String get(int type) {
        if (type == 0) {
            if (head == null) {
                return "-1";
            }
            type = head.type;
        }
        if (!hashMap.containsKey(type)) {
            return "-1";
        }
        LinkedList<Entity> list = hashMap.get(type);
        Entity entity = list.removeFirst();
        if (list.isEmpty()) {
            hashMap.remove(type);
        }
        if (head == entity && tail == entity) {
            head = tail = null;
        } else if (head == entity) {
            removeHead();
        } else if (entity == tail) {
            removeTail();
        } else {
            entity.pre.next = entity.next;
            entity.next.pre = entity.pre;
        }
        return entity.message;
    }

    public void put(int type, String value) {
        Entity entity = new Entity(type, value);
        if (tail == null) {
            head = tail = entity;
        } else {
            entity.pre = tail;
            tail.next = entity;
            tail = entity;
        }
        LinkedList<Entity> list = hashMap.computeIfAbsent(type, k->new LinkedList<>());
        list.add(entity);
    }

    private void removeHead() {
        if (head == tail) {
            head = tail = null;
        } else {
            head = head.next;
            head.pre = null;
        }
    }

    private void removeTail() {
        if (head == tail) {
            head = tail = null;
        } else {
            tail = tail.pre;
            tail.next = null;
        }
    }

    private static class Entity {
        int type;
        String message;
        Entity pre;
        Entity next;

        public Entity(int type, String message) {
            this.type = type;
            this.message = message;
        }
    }
}



#莉莉丝游戏##笔经#
全部评论
有无大佬贴一个第三题的C++的代码,感谢
点赞 回复 分享
发布于 2021-09-06 22:30
膜拜大佬
点赞 回复 分享
发布于 2021-09-06 21:09

相关推荐

01-17 18:15
已编辑
门头沟学院 前端工程师
从上午约我面试然后他迟到,然后中午发消息打电话给我说重约面试时间,我就该意识到。【管理不规范,只是这家公司最小的问题】他妈一个不是技术的人来给我技术面。。。连vvue什么?连react是什么?连普通的HTTP请求是什么?这些东西都不懂的人来给我做技术面,我真的。。。。他妈浪费我40分钟。。一天面了三场,这家公司属实牛逼。不停的问我说上班下班时间谁来派任务公司在哪个区发展怎么样,公司的管理模式什么样,培养机制怎么样带教负责什么。如果出bug了谁来负责。我真的求你了别闹了。我答了15分钟,我已经很不想回答了。然后他就问了我一些很招笑的面试问题。问我前端框架架构设计怎么设计,Websocket可以实现SSE吗??最后还要我硬说,为什么我们公司没转正?为什么?为什么?我说我怎么知道。。这是领导决定,又不是我决定,他说让我分析一下。。。我真的草了,这个人是来搞我的吗?我最后问我说这个没有技术面,他说他就是技术面虽然我今天面的另外两家也很逆天。一个人不停的吹牛,自己100人的公司是全国前几,吹牛了一个小时。我中途几次想跑,真的是底下玩手机在听他那吹牛。。然后最后来了句说,我承诺的东西要实现哦,不然的话,公司会追责的,我我请问我承诺了什么?从头到尾也没有说让我承诺什么。而且我只是作为一个小小的前端卡拉咪,应届生。我要承担什么??好崩溃。。好崩溃的,一天面了三场。两家1000-9999的公司。面试官问的都很傻逼,甚至有些东西我问他估计都答不出来。。&nbsp;我这是在干嘛呀?浪费我一天的时间,我的奶奶。。我本来是抱着说我很菜,我要面试中发现自己的问题,现在来看他妈的这三场面试,面试本身就是问题。。
点赞 评论 收藏
分享
评论
6
16
分享

创作者周榜

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