首页 > 其他分享 >2*STACK = QUEUE √ , 2*QUEUE = STACK?吗

2*STACK = QUEUE √ , 2*QUEUE = STACK?吗

时间:2022-12-06 00:56:09浏览次数:21  
标签:queue return int queueIn public QUEUE queueBackUp STACK

232. 用栈实现队列

class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    public MyQueue() {
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }

    public void push(int x) {
        stackIn.push(x);
    }

    public int pop() {
        stackChange();
        Integer pop = stackOut.pop();
        return pop;
    }

    public int peek() {
        stackChange();
        Integer peek = stackOut.peek();
        return peek;
    }

    public boolean empty() {
        return stackOut.isEmpty() && stackIn.isEmpty();
    }

    public void stackChange() {
        if(!stackOut.isEmpty()) return; // 自己的代码少了这里
        while (!stackIn.isEmpty()) {
            Integer head = stackIn.pop();
            stackOut.push(head);
        }
    }
}

所以说!!! 思路很重要

225. 用2个队列实现栈

这个写了15分钟写出来

class MyStack {
       Queue<Integer> queueIn;
    Queue<Integer> queueBackUp;

    public MyStack() {
        queueIn = new LinkedList<>();
        queueBackUp = new LinkedList<>();
    }

    public void push(int x) {
        reBackUp();
        queueIn.offer(x);
    }

    public int pop() {
        reBackUp();
        while (queueIn.size() > 1) {
            Integer front = queueIn.poll();
            queueBackUp.offer(front);
        }
        return queueIn.poll();
    }

    public int top() {
        reBackUp();
        while (queueIn.size() > 1) {
            Integer front = queueIn.poll();
            queueBackUp.offer(front);
        }
        return queueIn.peek();

    }

    public boolean empty() {
        return queueIn.size() == 0 && queueBackUp.size() == 0;
    }

    private void reBackUp() {
        while (queueIn.size() != 0) {
            queueBackUp.offer(queueIn.poll());
        }
        Queue<Integer> tmp = queueIn;
        queueIn = queueBackUp;
        queueBackUp = tmp;
    }
}

其实第二个队列是纯纯的 backup,可以不要

这个写了3分钟写出来

实际上,一个数(假定它叫X)入队之后, 是可以 把X前面的 全部 移到X的后面.

class MyStack {
    Queue<Integer> queue = new LinkedList<>();

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.offer(x);
        int move = queue.size() - 1; // 插入一个,前面就是0个,再插入一个前面就是1个
        for (int i = 0; i < move; i++) {
            queue.offer(queue.poll());
        }

    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}

标签:queue,return,int,queueIn,public,QUEUE,queueBackUp,STACK
From: https://www.cnblogs.com/Chain-Tian/p/16954030.html

相关文章

  • LeetCode: 225. Implement Stack using Queues
    LeetCode:225.ImplementStackusingQueues题目描述Implementthefollowingoperationsofastackusingqueues.​​push(x)​​–Pushelementxontostack.​......
  • LeetCode: 232. Implement Queue using Stacks
    LeetCode:232.ImplementQueueusingStacks题目描述Implementthefollowingoperationsofaqueueusingstacks.​​push(x)​​​–Pushelementxtothebacko......
  • openstack安装(超详细)
    1、系统环境配置2、keystone(认证)3、glance(镜像)4、compute(计算)5、network(网络)6、dashboard(仪表盘)7、cinder(块存储)8、部署、对接ceph存储集群9、运营、......
  • throw e和e.printStackTrace()的区别
    throwe是抛出异常,会中断程序,后面的代码都不执行了e.printStackTrace()是输出错误日志,并不中断程序如下代码来说明用法:/***throwe和e.printStackTrace()的区别*......
  • npm 报错:npm ERR! Maximum call stack size exceeded 超过最大栈问题解决方案
    先尝试将npm升级到最新版1234$npminstallnpm-g#检查版本$npm-v6.14.5运行以下命令删除当前路径下的node_modules目录1234$rmnode_mo......
  • java 中的Stack、Queue、Deque
    1.Stackjava集合框架中没有Stack接口,仅仅有java早期遗留下来的一个Stack类。Dequestack=newArrayDeque();publicStackextendsVector因为集成自Vector,所以Stack类是......
  • starctf2018_babystack
    starctf2018_babystack新知识这道题又学到一个知识点,就是有关控制canary,之前所了解的就是他会生成一个随机数,然会放到一个寄存器中如下图64位时fs:28,32位时fs:14,在返回......
  • ConConcurrentQueue Testing
    //ProvidesaC++11implementationofamulti-producer,multi-consumerlock-freequeue.//Anoverview,includingbenchmarkresults,isprovidedhere://http......
  • Python: stack
     Astackisalineardatastructurethatstoresitemsinalast-in-first-outLIFOorfirst-in-last-outFILOmanner,Instack,anewelementisaddedatonee......
  • the--openssl升级引起的openstack服务异常问题
    1.问题查看openstack服务,各个服务报错提示:  PAMunabletodlopen(/lib64/security/pam_tally.so):/lib64/security/pam_tally.so:cannotopensharedobjectfile......