首页 > 编程语言 >算法学习day10栈与队列part01-232、225

算法学习day10栈与队列part01-232、225

时间:2023-05-09 23:22:34浏览次数:34  
标签:part01 pop queue 队列 int day10 push 225 public

package LeetCode.StackAndQueuepart01;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 225. 用队列实现栈
 * 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
 * 实现 MyStack 类:
 * void push(int x) 将元素 x 压入栈顶。
 * int pop() 移除并返回栈顶元素。
 * int top() 返回栈顶元素。
 * boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
 * 注意:
 * 你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
 * 你所使用的语言也许不支持队列。你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列, 只要是标准的队列操作即可。
 * 示例:
 * 输入:
 * ["MyStack", "push", "push", "top", "pop", "empty"]
 * [[], [1], [2], [], [], []]
 * 输出:
 * [null, null, null, 2, 2, false]
 *
 * */
public class ImplementStackUsingQueues_225 {
    public static void main(String[] args) {
        // 需要看一下数据结构

    }


}
class MyStack {

    Queue<Integer> queue;

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

    //每 offer 一个数(A)进来,都重新排列,把这个数(A)放到队列的队首
    public void push(int x) {
        queue.offer(x);
        int size = queue.size();
        //移动除了 A 的其它数
        while (size-- > 1)
            queue.offer(queue.poll());
    }

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

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

    public boolean empty() {
        return queue.isEmpty();
    }
}
package LeetCode.StackAndQueuepart01;
import java.util.Stack;

/**
 * 232. 用栈实现队列
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
 * 实现 MyQueue 类:
 * void push(int x) 将元素 x 推到队列的末尾
 * int pop() 从队列的开头移除并返回元素
 * int peek() 返回队列开头的元素
 * boolean empty() 如果队列为空,返回 true ;否则,返回 false
 * 说明:
 * 你 只能 使用标准的栈操作 —— 也就是只有 push to top,peek/pop from top, size, 和 is empty 操作是合法的。
 * 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
 * 示例:
 * 输入:
 * ["MyQueue", "push", "push", "peek", "pop", "empty"]
 * [[], [1], [2], [], [], []]
 * 输出:
 * [null, null, null, 1, 1, false]
 * */
public class ImplementQueueUsingStacks_232 {
    public static void main(String[] args) {

    }
}
class MyQueue {

    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {
        stackIn = new Stack<>(); // 负责进栈
        stackOut = new Stack<>(); // 负责出栈
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        stackIn.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        dumpstackIn();
        return stackOut.pop();
    }

    /**
     * Get the front element.
     */
    public int peek() {
        dumpstackIn();
        return stackOut.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }

    // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
    private void dumpstackIn() {
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
        }
    }
}

 

标签:part01,pop,queue,队列,int,day10,push,225,public
From: https://www.cnblogs.com/lipinbigdata/p/17386658.html

相关文章

  • 题解 P3225 [HNOI2012] 矿场搭建
    解析传送门一道简单的tarjan题题意:在无向图中找一些点,这些点组成的的点集记为\(V\),使得去掉任意一个点,剩下的每一个点都可以到达\(V\)中任意一个点,求点集\(V\)的大小的最小值及其方案数。去掉一个点,很自然的联想到割点,那么考虑一下割点在不在备选集合中。如图,显然可以看出,......
  • 完整实现React day10
    update流程与mount流程的区别。对于beginWork:需要处理ChildDeletion的情况需要处理节点移动的情况(abc->bca)对于completeWork:需要处理HostText内容更新的情况需要处理HostComponent属性变化的情况对于commitWork:对于ChildDeletion,需要遍历被删除的子树对于Update,需......
  • Codeforces Round #225 (Div. 2) C. Milking cows Greedy
    Iahubhelpshisgrandfatheratthefarm.Todayhemustmilkthecows.Therearencowssittinginarow,numberedfrom1tonfromlefttoright.Eachcowiseitherfacingtotheleftorfacingtotheright.WhenIahubmilksacow,allthecowsthatseet......
  • codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)
    题目链接:codeforces225B题目大意:定义f(k,n)为类似菲波那契数推导,只不过变为前k项的和,然后给出一个数s,利用k-菲波那契数构造出一个不重复的集合的元素和为s,集合的规模大于1题目分析:首先因为菲波那契数的增长速度快的吓人,所以给的数据范围109很快就能达到,我们得到O(n)的构造出所有的......
  • 慧荣SM2259XT固件,SM2259XT开卡软件教程,慧荣SM2259XT主控贴镁光B27A测试分享
    上回说到把mas0902开不了的两片镁光降级(AS)B27A贴上了as2258,确实能开,缓内速度也可以,但是吧,大家说的全盘模拟SLC并没发现,也就前4g速度快,缓外20M写入(当然这是颗粒的问题),这速度能用个啥,缓内数据再漂亮也不能在实际使用体现出来啊,这是典型的花架子。如果有大佬教教我AS2258如何开全盘模......
  • 慧荣SM2259XT固件下载方法,分享个SM2259XT Intel N18混贴3CH开卡经验
    收了条Intel的512G不认盘的ssd,拆出来两颗29F02T2AMCQH1,这个应该是正品,ID也没问题。然后,还有个山寨的256GSATA,主控2259XT,两个颗粒丝印29F1TB2ALCTH2,但是,ID与CQH1一样,都是89,D4,0C,32,AA,00,应该是刮刮乐了,但是主控也不贴个58XT啥的,造假也太不负责了。 SM2259XT短接还认,用N18的工具......
  • 算法学习day03链表part01-203、707、206--待办
    //这块需求重新进行学习packageLeetCode.linkedlistpart01;publicclassListNode{//结点的值intval;//下一个结点ListNodenext;//节点的构造函数(无参)publicListNode(){}//节点的构造函数(有一个参数)publicLis......
  • 225 队列实现stack
         解决方法简单粗暴,把队列前面的都取出来再加入队尾,让之前的队尾元素排到队头,这样就可以取出了: importqueueclassMyStack:def__init__(self):self.q=queue.Queue()self.top_element=0defpush(self,x:int)->None:......
  • kuangbin专题一 简单搜索 地牢大师(POJ-2251)
    DungeonMasterTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:92499 Accepted:31990DescriptionYouaretrappedina3Ddungeonandneedtofindthequickestwayout!Thedungeoniscomposedofunitcubeswhichmayormaynotbefilledwith......
  • 剑指 Offer 09. 用两个栈实现队列 && leetcode225.用队列实现栈
     剑指Offer09.用两个栈实现队列 classCQueue{private:stack<int>inStack,outStack;voidin2out(){//这里必须是while循环,如果是if判断,则输出栈日常只有一个值,没有起到先入后出的作用while(!inStack.empty()){//将输入栈栈顶......