• 495 队列,优先级队列


    一、队列

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    
    </body>
    
    </html>
    <script>
        function Queue() {
            // 创建一个队列的容器
            this.container = [];
        }
        Queue.prototype = {
            constructor: Queue,
            // 进入队列  element进入队列的元素
            enter: function enter(element) {
                this.container.push(element);
            },
            // 移除队列
            leave: function leave() {
                if (this.container.length === 0) return;
                return this.container.shift();
            },
            // 查看队列的长度
            size: function size() {
                return this.container.length;
            },
            // 查看队列的内容
            value: function value() {
                // 深度克隆是为了保证后期外面接收到的结果不论如何的操作都不会影响内部容器中的内容
                // this.container.slice(0) 浅拷贝
                // JSON.parse(JSON.stringify(this.container)) 深拷贝
                return JSON.parse(JSON.stringify(this.container));
            }
        };
    
        // 创建一个队列
        let qe = new Queue();
        qe.enter(1);
        qe.enter(2);
        qe.enter(3);
        qe.enter(4);
        qe.enter(5);
        qe.leave();
        console.log(qe.value()); // [2, 3, 4, 5]
    
    
        // ------------
    
    
        /* 算法面试题:击鼓传花 */
        // n:参加游戏的人数  m:关键数
        function game(n, m) {
            let qe = new Queue();
    
            // 先把人都依次放入到队列中
            for (let i = 1; i <= n; i++) {
                qe.enter(i);
            }
    
            // 开始处理
            while (qe.size() > 1) {
                for (let i = 0; i < m - 1; i++) {
                    qe.enter(qe.leave());
                }
                qe.leave();
            }
    
            return qe.value()[0];
        }
        let res = game(6, 4);
        console.log(res); // 5
    </script>
    

    二、优先级队列

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    
    </body>
    
    </html>
    <script>
        function Queue() {
            this.container = [];
        }
    
        Queue.prototype = {
            constructor: Queue,
            // 进入队列  priority优先级,默认都是0,数值越大,优先级越高
            enter: function enter(element, priority = 0) {
                let obj = {
                    value: element,
                    priority: priority
                };
                if (priority === 0) {
                    // 不指定优先级(最小优先级):存储到末尾即可
                    this.container.push(obj);
                    return;
                }
                // 指定优先级,我们需要从最后一项依次来比较
                let flag = false;
                for (let i = this.container.length - 1; i >= 0; i--) {
                    let item = this.container[i];
                    if (item.priority >= priority) {
                        // 插入到比较项的后面,也就是它的下一项的前面 【从 i + 1 开始,删除0项,插入obj】
                        this.container.splice(i + 1, 0, obj);
                        flag = true;
                        break;
                    }
                };
                // 没有比我大的,我就是最大的,插入到容器最开始的位置即可
                !flag ? this.container.unshift(obj) : null;
            },
    
            // 移除队列
            leave: function leave() {
                if (this.container.length === 0) return;
                return this.container.shift();
            },
    
            // 查看队列的长度
            size: function size() {
                return this.container.length;
            },
    
            // 查看队列的内容
            value: function value() {
                return JSON.parse(JSON.stringify(this.container));
            }
        };
    
        let qe = new Queue();
        qe.enter(1);
        qe.enter(2);
        qe.enter(3);
        qe.enter(4);
        qe.enter(5);
        console.log(qe.value());
    
    </script>
    
  • 相关阅读:
    函数的重载 C++快速入门06
    PE格式详细讲解8 系统篇08|解密系列
    《零基础入门学习汇编语言》检测点,实验,课后题答案
    PE格式详细讲解9 系统篇09|解密系列
    C++输出输入小结 C++快速入门05
    使用XML生成菜单
    DNS解析过程详解
    Windows Azure 2.5天深度技术训练营 和 微软公有云发现之旅
    使用单例模式实现自己的HttpClient工具类
    android 反编译和防止被反编译。
  • 原文地址:https://www.cnblogs.com/jianjie/p/13219679.html
Copyright © 2020-2023  润新知