莉莉丝游戏笔试记录

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

相关推荐

辅助位:定时器项目都被用烂了,感觉
点赞 评论 收藏
分享
真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
评论
6
16
分享

创作者周榜

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