代码训练营 day 10|LeetCode 232,LeetCode 225,LeetCode 20,LeetCode 1047

前言

这里记录一下陈菜菜的刷题记录,主要应对25秋招、春招
个人背景
211CS本+CUHK计算机相关硕,一年车企软件开发经验
代码能力:有待提高
常用语言:C++

系列文章目录

第一天 数组 part01

第二天 数组 part02

第三天 链表 part01

第四天 链表 part02

第五天 休息

第六天 哈希表 part01

第七天:第三章 哈希表part02

第八天 :第四章 字符串part01

第九天 :第四章 字符串part02

第十天 :第五章 栈与队列part01


`



一、今日任务

● 理论基础
● 232.用栈实现队列
● 225. 用队列实现栈
● 20. 有效的括号
● 1047. 删除字符串中的所有相邻重复项

二、详细布置

232.用栈实现队列

题目链接:[力扣232]https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/)
文章讲解:代码随想录
视频讲解:代码随想录

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

实现 MyQueue 类:

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

提示:

1 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

样例1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false 
思路

这题简单题,两个栈实现。

实战
class MyQueue {
public:
    stack<int> s1;
    stack<int> s2;

    MyQueue() {
        
    }
    
    void push(int x) {
        s1.push(x);
    }
    
    int pop() {
        int len=s1.size();
        int i;
        for(i=0;i<len-1;i++){
            s2.push(s1.top());
            s1.pop();
        }
        int result=s1.top();
        s1.pop();
        for(i=0;i<len-1;i++){
            s1.push(s2.top());
            s2.pop();
        }
        return result;
    }
    
    int peek() {
        int len=s1.size();
        int i;
        for(i=0;i<len-1;i++){
            s2.push(s1.top());
            s1.pop();
        }
        int result=s1.top();
        //s1.pop();
        for(i=0;i<len-1;i++){
            s1.push(s2.top());
            s2.pop();
        }
        return result;
    }
    
    bool empty() {
        if(s1.size()!=0)
            return false;
        return true;
    }
};

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

225. 用队列实现栈

题目链接:力扣225题链接
文章讲解:图文讲解

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

实现 MyStack 类:

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

提示:

1 <= x <= 9
最多调用100 次 push、pop、top 和 empty
每次调用 pop 和 top 都保证栈不为空

样例1:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]

解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False     
思路

这题简单题。

实战
class MyStack {
public:
    queue<int> q1;
    queue<int> q2;
    MyStack() {
        
    }
    
    void push(int x) {
         q1.push(x);
         //return result;       
    }
    
    int pop() {
        int len=q1.size();
         int i;
         for(i=0;i<len-1;i++){
            q2.push(q1.front());
            q1.pop();
         }
         int result=q1.front();
         q1.pop();
         for(i=0;i<len-1;i++){
            q1.push(q2.front());
            q2.pop();
         }
         return result;
    }
    
    int top() {
        int len=q1.size();
        int i;
         for(i=0;i<len-1;i++){
            q2.push(q1.front());
            q1.pop();
         }
        int result=q1.front();
        q2.push(q1.front());
        q1.pop();
         for(i=0;i<len;i++){
            q1.push(q2.front());
            q2.pop();
         }
         return result;
    }
    
    bool empty() {
        if(q1.size()!=0)
            return false;
        return true;
    }
};

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

queue的对头元素是front,顺便STL真好用啊!!

20. 有效的括号

题目链接:LeetCode20
文章讲解:图文讲解

题目描述

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

有效字符串需满足:

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

提示:

1 <= s.length <= 104
s 仅由括号 ‘()[]{}’ 组成

样例1:
输入:s = "()"

输出:true
样例2:
输入:s = "()[]{}"

输出:true
思路

用栈做,左括号入栈,遇到右括号看栈顶元素是否是与之匹配的左括号,是就匹配,否则不匹配。

实战
class Solution {
public:
    bool isValid(string s) {
        stack<char> mystack;
        int i;
        for(i=0;i<s.size();i++){
            if(s[i]=='('||s[i]=='{'||s[i]=='[')
                mystack.push(s[i]);
            else{
                char t=mystack.top();
                if(s[i]==')'&&t!='('||s[i]==']'&&t!='['||s[i]=='}'&&t!='{')
                    return false;
                else
                    mystack.pop();
            }
        }
        if(mystack.size()!=0)
            return false;
        return true;
    }
};

1047. 删除字符串中的所有相邻重复项

题目链接:1047
文章讲解:图文讲解

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

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

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

提示:

1 <= s.length <= 105
s 仅由小写英文字母组成。

样例1:
输入:"abbaca"
输出:"ca"
解释:
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"
思路

栈。

实战
class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> mystack;
        int len=s.size();
        int i;
        //mystack.push(s[0]);
        for(i=0;i<len;i++){
            if(mystack.empty()||mystack.top()!=s[i])
                mystack.push(s[i]);
            else{
                mystack.pop();
            }
        }
        string re;
        int llen=mystack.size();
        for(i=0;i<llen;i++){
        //while(!mystack.empty()){
            re+=mystack.top();
            mystack.pop();
        }
        reverse(re.begin(),re.end());
        return re;

    }
};

总结

今天主要学习了栈和队列的一系列操作,感觉是比较熟悉的内容,做起来很快。
加油,坚持打卡的第十天。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值