LOADING

加载过慢请开启缓存 浏览器默认开启

2023/11/20

每日三题(用栈实现队列/用队列实现栈+有效的括号)

好久没写算法了,狠狠复健一下

//用两个栈实现队列/用两个队列实现栈:我的解法异曲同工,都是把另一个容器当作临时存储空间来调整主容器的顺序
// Created by 徐昊岩 on 2023/11/20.
//有效的括号,一眼栈,秒了
#include "bits/stdc++.h"
using namespace std;
class MyQueue {
public:
    stack<int> stack_main;
    stack<int> stack_temp;
    MyQueue() {

    }

    void push(int x) {
        if(stack_main.empty()){
            stack_main.emplace(x);
        } else{
            while (!stack_main.empty()) {
                stack_temp.emplace(stack_main.top());
                stack_main.pop();
            }
            stack_main.emplace(x);
            while (!stack_temp.empty()){
                stack_main.emplace(stack_temp.top());
                stack_temp.pop();
            }
        }

    }

    int pop() {
        int a =stack_main.top();
        stack_main.pop();
        return a;
    }

    int peek() {
        return stack_main.top();
    }

    bool empty() {
        return stack_main.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

class MyStack {
public:
    queue<int> queue_main;
    queue<int> queue_temp;
    MyStack() {

    }

    void push(int x) {
        if(queue_main.empty()){
            queue_main.emplace(x);
        }else {
            while (!queue_main.empty()){
                queue_temp.emplace(queue_main.front());
                queue_main.pop();
            }
            queue_main.emplace(x);
            while (!queue_temp.empty()){
                queue_main.emplace(queue_temp.front());
                queue_temp.pop();
            }
        }
    }

    int pop() {
        int a=queue_main.front();
        queue_main.pop();
        return a;
    }

    int top() {
        return queue_main.front();
    }

    bool empty() {
        return queue_main.empty();
    }
};

class Solution {
public:
    bool isValid(string s) {
        if(s.size()%2!=0) return false;
        stack<char> stack;
        for(char c:s){
            if(c=='('||c=='['||c=='{' ) stack.emplace(c);
            else{
                if(stack.size()<1) return false;
                else{
                    char right=stack.top();
                    switch (c) {
                        case ')':
                            if(right=='(') stack.pop();
                            else return false;
                            break;
                        case ']':
                            if(right=='[') stack.pop();
                            else return false;
                            break;
                        case '}':
                            if(right=='{') stack.pop();
                            else return false;
                            break;

                    }
                }
            }
        }

        return stack.size() == 0;
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */