• LeetCode 栈和队列


    基础部分

    232. 用栈实现队列

    简单

    使用栈实现队列的下列操作:

    • push(x) -- 将一个元素放入队列的尾部。
    • pop() -- 从队列首部移除元素。
    • peek() -- 返回队列首部的元素。
    • empty() -- 返回队列是否为空。

    示例:

    MyQueue queue = new MyQueue();
    
    queue.push(1);
    queue.push(2);  
    queue.peek();  // 返回 1
    queue.pop();   // 返回 1
    queue.empty(); // 返回 false
    

    说明:

    • 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
    • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
    • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
    class MyQueue {
        Stack<Integer> stack1;
        Stack<Integer> stack2;
    
        public MyQueue() {
            stack1 = new Stack<>();
            stack2 = new Stack<>();
        }
    
        public void push(int x) {
            while (!stack2.isEmpty()){
                stack1.push(stack2.pop());
            }
            stack1.push(x);
        }
        
        public int pop() {
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
        
        public int peek() {
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
        
        public boolean empty() {
            return stack1.isEmpty() && stack2.isEmpty();
        }
    }
    

    225. 用队列实现栈

    简单

    使用队列实现栈的下列操作:

    • push(x) -- 元素 x 入栈
    • pop() -- 移除栈顶元素
    • top() -- 获取栈顶元素
    • empty() -- 返回栈是否为空

    注意:

    • 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
    • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
    • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
    class MyStack {
        Queue<Integer> queue1;
        Queue<Integer> queue2;
    
        public MyStack() {
            queue1 = new LinkedList<>();
            queue2 = new LinkedList<>();
        }
        
        public void push(int x) {
            queue1.add(x);
        }
        
        public int pop() {
            while (queue1.size()>1){
                queue2.add(queue1.poll());
            }
            int ans = queue1.poll();
            Queue<Integer> tmp = queue1;
            queue1 = queue2;
            queue2 = tmp;
            return ans;
        }
        
        public int top() {
            while (queue1.size()>1){
                queue2.add(queue1.poll());
            }
            int ans = queue1.peek();
            queue2.add(queue1.poll());
            Queue<Integer> tmp = queue1;
            queue1 = queue2;
            queue2 = tmp;
            return ans;
        }
    
        public boolean empty() {
            return queue1.isEmpty();
        }
    }
    

    155. 最小栈

    简单

    设计一个支持 pushpoptop 操作,并能在常数时间内检索到最小元素的栈。

    • push(x) —— 将元素 x 推入栈中。
    • pop() —— 删除栈顶的元素。
    • top() —— 获取栈顶元素。
    • getMin() —— 检索栈中的最小元素。

    示例:

    输入:
    ["MinStack","push","push","push","getMin","pop","top","getMin"]
    [[],[-2],[0],[-3],[],[],[],[]]
    
    输出:
    [null,null,null,null,-3,null,0,-2]
    
    解释:
    MinStack minStack = new MinStack();
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    minStack.getMin();   --> 返回 -3.
    minStack.pop();
    minStack.top();      --> 返回 0.
    minStack.getMin();   --> 返回 -2.
    

    提示:

    • poptopgetMin 操作总是在 非空栈 上调用。
    class MinStack { // 7ms
        Stack<Integer> stack;
        Stack<Integer> minstack;
        int min;
    
        public MinStack() {
            stack = new Stack<>();
            minstack = new Stack<>();
            min = Integer.MAX_VALUE;
        }
        
        public void push(int x) {
            stack.push(x);
            if (x < min) min = x;
            minstack.push(min);
        }
        
        public void pop() {
            stack.pop();
            minstack.pop();
            min = minstack.isEmpty() ? Integer.MAX_VALUE : minstack.peek();
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int getMin() {
            return minstack.peek();
        }
    }
    

    20. 有效的括号

    简单

    给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

    有效字符串需满足:

    1. 左括号必须用相同类型的右括号闭合。
    2. 左括号必须以正确的顺序闭合。

    注意空字符串可被认为是有效字符串。

    示例 1:

    输入: "()"
    输出: true
    

    示例 2:

    输入: "()[]{}"
    输出: true
    

    示例 3:

    输入: "(]"
    输出: false
    

    示例 4:

    输入: "([)]"
    输出: false
    

    示例 5:

    输入: "{[]}"
    输出: true
    
    class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            for (char c : s.toCharArray()){
                if (c == '(' || c == '[' || c == '{'){
                    stack.push(c);
                }else if (c == ')'){
                    if (stack.isEmpty() || stack.pop() != '(') return false;
                }else if (c == ']'){
                    if (stack.isEmpty() || stack.pop() != '[') return false;
                }else {
                    if (stack.isEmpty() || stack.pop() != '{') return false;
                }
            }
            return stack.isEmpty();
        }
    }
    

    739. 每日温度

    中等

    请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。

    例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]

    提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。

    动画演示

    class IndexAndNum{
        int index;
        int num;
        public IndexAndNum(int index,int num){
            this.index = index;
            this.num = num;
        }
    }
    class Solution {
        public int[] dailyTemperatures(int[] T) {
            int n = T.length;
            int[] res = new int[n];
            Stack<IndexAndNum> stack = new Stack<>();
            stack.push(new IndexAndNum(0, T[0]));
            for (int i = 1; i < n; i++){
                while (!stack.isEmpty() && T[i] > stack.peek().num){
                    int index = stack.pop().index;
                    res[index] = i - index;
                }
                stack.push(new IndexAndNum(i, T[i]));
            }
            return res;
        }
    }
    

    503. 下一个更大元素 II

    中等

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。

    示例 1:

    输入: [1,2,1]
    输出: [2,-1,2]
    解释: 第一个 1 的下一个更大的数是 2;
    数字 2 找不到下一个更大的数; 
    第二个 1 的下一个最大的数需要循环搜索,结果也是 2。
    

    注意: 输入数组的长度不会超过 10000。

    class IndexAndNum{
        int index;
        int num;
        public IndexAndNum(int index,int num){
            this.index = index;
            this.num = num;
        }
    }
    class Solution {
        public int[] nextGreaterElements(int[] nums) {
            int n = nums.length;
            int[] res = new int[n];
            Arrays.fill(res, -1);
            Stack<IndexAndNum> stack = new Stack<>();
            int i, j;
            for (j = 0; j < 2*n; j++){ //两轮循环
                i = j % n;
                while (!stack.isEmpty() && nums[i] > stack.peek().num){
                    int index = stack.pop().index;
                    res[index] = nums[i];
                }
                stack.push(new IndexAndNum(i, nums[i]));
            }
            return res;
        }
    }
    

    频率排序

    394,42,726,316,85,975,385,862,402,84,456

  • 相关阅读:
    今天博客开通第一天,以此纪念!
    基于opencv的车牌识别系统
    【C和指针】笔记1
    【局域网聊天客户端篇】基于socket与Qt
    对Qt下对话服务器客户端的总结(MyTcpServer与MyTcpClient)
    linux 线程编程详解
    【linux】安装samba服务
    linux(ubuntu)获取命令源码方式
    win7下用SSH连接linux虚拟机
    Linux下deb包安装工具(附带安装搜狗输入法)
  • 原文地址:https://www.cnblogs.com/peng8098/p/leetcode11.html
Copyright © 2020-2023  润新知