首页 > 其他分享 >leetcode-225-easy

leetcode-225-easy

时间:2023-01-09 22:11:57浏览次数:43  
标签:int top stack easy queueOne push 225 leetcode empty

Implement Stack using Queues

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

void push(int x) Pushes element x to the top of the stack.
int pop() Removes the element on the top of the stack and returns it.
int top() Returns the element on the top of the stack.
boolean empty() Returns true if the stack is empty, false otherwise.
Notes:

You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
Example 1:

Input
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 2, 2, false]

Explanation
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // return 2
myStack.pop(); // return 2
myStack.empty(); // return False
Constraints:

1 <= x <= 9
At most 100 calls will be made to push, pop, top, and empty.
All the calls to pop and top are valid.
Follow-up: Can you implement the stack using only one queue?

思路一:两个队列,一个用来存储所有值,另外一个队列只存储单个最新的值

    private Queue<Integer> queueFull;
    private Queue<Integer> queueOne;

    public MyStack() {
        queueFull =  new ArrayDeque<>();
        queueOne =  new ArrayDeque<>();
    }

    public void push(int x) {
        queueFull.offer(x);
        queueOne.clear();
        queueOne.offer(x);
    }

    public int pop() {
        int val = queueOne.isEmpty() ? -1 : queueOne.poll();

        int last = -1;
        while (queueFull.size() > 1) {
            last = queueFull.poll();
            queueOne.offer(last);
        }
        queueFull = queueOne;
        queueOne = new ArrayDeque<>();
        if (last != -1) queueOne.offer(last);

        return val;
    }

    public int top() {
        return queueOne.isEmpty() ? -1 : queueOne.peek();
    }

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

思路二:看了一下官方的解法,发现思路不一样,每次用一个队列先入新增值,然后把另外一个队列的所有值入队列。这样的算法会简洁一些

标签:int,top,stack,easy,queueOne,push,225,leetcode,empty
From: https://www.cnblogs.com/iyiluo/p/17038672.html

相关文章

  • leetcode简单:[1, 9, 13, 14, 20, 21, 26, 27, 35, 58]
    目录1.两数之和9.回文数13.罗马数字转整数14.最长公共前缀20.有效的括号21.合并两个有序链表26.删除有序数组中的重复项27.移除元素35.搜索插入位置58.最后一个......
  • leetcode简单:[66, 67, 70, 83, 121, 141, 160, 169, ,206, 338]
    目录66.加一67.二进制求和70.爬楼梯83.删除排序链表中的重复元素121.买卖股票的最佳时机141.环形链表160.相交链表169.多数元素206.反转链表338.比特位计数66.......
  • LeetCode.977 有序数组的平方
    1.题目给你一个按 非递减顺序 排序的整数数组 ​​​​nums​​​​,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。2.代码classSolution{public......
  • LeetCode 46_ 全排列
    LeetCode46:全排列题目给定一个不含重复数字的数组nums,返回其所有可能的全排列。你可以按任意顺序返回答案。示例1:输入:nums=[1,2,3]输出:[[1,2,3],[1,3,2],......
  • 242. Valid Anagram [Easy]
    242.ValidAnagramGiventwostringssandt,returntrueiftisananagramofs,andfalseotherwise.AnAnagramisawordorphraseformedbyrearrangingth......
  • [oeasy]python0041_ 转义字符_转义序列_escape_序列_sequence
    转义序列回忆上次内容上次回顾了5bit-Baudot博多码的来历从莫尔斯码到博多码原来人来收发电报现在机器来收发电报输入方式从电键改成键盘......
  • 217. Contains Duplicate [Easy]
    217.ContainsDuplicateGivenanintegerarraynums,returntrueifanyvalueappearsatleasttwiceinthearray,andreturnfalseifeveryelementisdistinc......
  • LeetCode_单周赛_327
    目录6283.正整数和负整数的最大计数代码6285.执行K次操作后的最大分数代码6284.使字符串总不同字符的数目相等代码6283.正整数和负整数的最大计数代码直接遍历统......
  • 【LeetCode数组#5行为模拟】螺旋矩阵II
    螺旋矩阵II力扣题目链接(opensnewwindow)给定一个正整数n,生成一个包含1到n^2所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。示例:输入:3输出:[[1,......
  • [GYCTF2020]Easyphp
    [GYCTF2020]Easyphp考点:反序列化的对象逃逸非常典型的登陆界面,随便输了输,发现存在admin用户,猜测是弱口令,拿字典跑了一遍,无果按照以往的做题经验,觉得可能有源码泄露,尝试/......