代码随想录打卡第十天

30、用栈实现队列

力扣原题

题目简介:

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

初见思路:

​ 我首先想到的就是两个栈来实现,准备一个空栈2,准备一个正常栈1;我的目标就是保证栈1里的数就是正常的先入先出。怎么保证呢,每次要压入新的数到栈1的时候,我就先将栈1的数压入到栈2去,然后将数压入到栈2的最上面,再把栈2的数押入到栈1,这样子新压入的数就自然而然到了栈1的最底部,这样就实现了后入后出了。

​ 但我写的时候就觉得,肯定还有很多种逻辑来实现这个过程,但我觉得我这个还挺简单的。

class MyQueue {
public:
    MyQueue() {
        
    }
    
    void push(int x) {

        while(!stack1.empty()){
            int temp = stack1.top();
            stack1.pop();
            stack2.push(temp);
        }
        stack2.push(x);
        while(!stack2.empty()){
            int temp = stack2.top();
            stack2.pop();
            stack1.push(temp);
        }
    }
    
    int pop() {
        
        int temp =stack1.top();
        stack1.pop();
        return temp;
    }
    
    int peek() {
        return stack1.top();
    }
    
    bool empty() {
        return stack1.empty();
    }
private: 
    stack<int> stack1;
    stack<int> stack2;


};

/**
 * 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();
 */

31、用队列实现栈

力扣原题

题目简介:

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false

初见思路:

​ 还是先准备两个队列,入队的时候就是正常的入队,但是出队的时候,我就将前面的数全都先入队到另一个空队列,只留下最后的一个,再把这个数给pop出去就好了,这样就实现了后入先出。

class MyStack {
public:
    MyStack() {
        
    }
    
    void push(int x) {
        if(queue1.empty()){
            queue2.push(x);
        }else{
            queue1.push(x);
        }
    }
    
    int pop() {
            if(queue1.empty()){
            while(queue2.size() != 1){
                int temp = queue2.front();
                queue2.pop();
                queue1.push(temp);
            }
            int temp = queue2.front();
            queue2.pop();
            return temp;
        }else{
            while(queue1.size() != 1){
                int temp = queue1.front();
                queue1.pop();
                queue2.push(temp);
            }
            int temp = queue1.front();
            queue1.pop();
            return temp;
        }
    }
    
    int top() {
                if(queue1.empty()){
            while(queue2.size() != 1){
                int temp = queue2.front();
                queue2.pop();
                queue1.push(temp);
            }
            int temp = queue2.front();
            queue2.pop();
            queue1.push(temp);
            return temp;
        }else{
            while(queue1.size() != 1){
                int temp = queue1.front();
                queue1.pop();
                queue2.push(temp);
            }
            int temp = queue1.front();
            queue1.pop();
            queue2.push(temp);
            return temp;
        }
    }
    
    bool empty() {
        return queue1.empty() && queue2.empty();
    }
private:
    queue<int> queue1;
    queue<int> queue2;

};

/**
 * 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();
 */

32、括号匹配

力扣原题

题目简介:

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

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。

初见思路:

​ 我准备三个栈呗,一个栈检查一种括号,一个左括号,我就压入一个左括号,遇到一个右括号我就出一个左括号,然后我检查三个栈是不是都是空的就好了。

我去试试。

class Solution {
public:
    bool isValid(string s) {
        stack<char> stack1;//检查{}
        stack<char> stack2;//检查()
        stack<char> stack3;//检查[]
        
        int n = s.size();
        for(auto a : s){
            if(a=='{'){
                stack1.push('{');
            }
            else if(a=='('){
                stack1.push('(');
            }
            else if(a=='['){
                stack1.push('[');
            }
            else if(a=='}'){
                if(!stack1.empty()){
                   char temp =  stack1.top();
                   stack1.pop();
                   if(temp == '{'){
                    continue;
                   }
                   else{
                    return false;
                   }
                }
                else{
                    return false;
                } 
            }
            else if(a==')'){
                if(!stack1.empty()){
                   char temp =  stack1.top();
                   stack1.pop();
                   if(temp == '('){
                    continue;
                   }
                   else{
                    return false;
                   }
                }
                else{
                    return false;
                }    
            }
            else if(a==']'){
                if(!stack1.empty()){
                   char temp =  stack1.top();
                   stack1.pop();
                   if(temp == '['){
                    continue;
                   }
                   else{
                    return false;
                   }
                }
                else{
                    return false;
                } 
            }
        }
        return stack1.empty() && stack2.empty() && stack3.empty();
    }
};

​ 一般还不行啊,还得考虑顺序啊,一般还不行。嗯,还要按顺序pop出去,检查是什么。

33、删除字符串中所有相邻重复项

力扣原题

题目简介:

​ 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。

在 S 上反复执行重复项删除操作,直到无法继续删除。

在完成所有重复项删除操作后返回最终的字符串。答案保证唯一

初见思路:

​ 我没啥思路,可恶,我钻到牛角尖去了,想着怎么去修改原字符串种的元素,结果完全不知道该怎么记录,也不知道怎么删除。

算法思路:

​ 原来栈还有这种使用方法啊,实在是妙。

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> stack1;
        for(auto a : s){
            if(!stack1.empty()){
                char temp = stack1.top();
                if(temp == a){
                    stack1.pop();
                }else{
                    stack1.push(a);
                }
            }else{
                stack1.push(a);
            }
        }
        string res;
        while(!stack1.empty()){
            char temp = stack1.top();
            stack1.pop();
            res.push_back(temp);
        }
        int left =0;
        int right = res.size()-1;
        while(left < right){
            swap(res[left],res[right]);
            left++;
            right--;
        }

        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值