Leetcode刷题笔记13:【232】用栈实现队列【496】下一个更大元素 I【682】棒球比赛【844】

第十一天 2021-3-13 每日刷四题
刷题模块:栈 - 简单难度

一、【题】用栈实现队列

与用队列实现栈大同小异,在push()时维持入栈的元素放入栈底即可。

入栈时将其他元素放入辅助栈,将元素放入主栈,再将其他元素放回主栈。

stack<int> mStack,aStack;
void push(int x) {
    while(!mStack.empty()){
        aStack.push(mStack.top());
        mStack.pop();
    }
    mStack.push(x);
    while(!aStack.empty()){
        mStack.push(aStack.top());
        aStack.pop();
    }
}

二、【题】下一个更大元素 I(解)

初步思路是对的,使用unordered_map来保存每一个元素的更大元素,但是没想到如何在O(n)的时间复杂度内寻找。

官方使用单调栈,巧妙的一种算法

在这里插入图片描述

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int> hash;
        stack<int> single;
        vector<int> result;
		
		//单调栈算法,计算每一个元素的下一个更大元素
        int n=nums2.size();
        single.push(nums2[0]);
        for(int i=1;i<n;i++){
            if(nums2[i] <= single.top()) single.push(nums2[i]);
            else{
                do{
                    hash[single.top()]=nums2[i];
                    single.pop();
                }while(!single.empty() && nums2[i] > single.top());
                single.push(nums2[i]);
            }
        }
        //栈中剩余元素的处理
        while(!single.empty()){
            hash[single.top()]=-1;
            single.pop();
        }
		//生成结果
        n=nums1.size();
        for(int i=0;i<n;i++){
            result.push_back(hash[nums1[i]]);
        }

        return result;
    }
};

三、【题】棒球比赛

基础题,无难度和难点

class Solution {
public:
    int calPoints(vector<string>& ops) {
        stack<int> result;

        int n=ops.size();
        for(int i=0;i<n;i++){
            if(ops[i]=="C"){
                result.pop();
            }else if(ops[i]=="D"){
                result.push(result.top()*2);
            }else if(ops[i]=="+"){
                int a=result.top();
                result.pop();
                int b=result.top();
                result.push(a);
                result.push(a+b);
            }else{
                result.push(atoi(ops[i].c_str()));
            }
        }

        int sum=0;
        while(!result.empty()){
            sum+=result.top();
            result.pop();
        }
        return sum;
    }
};

四、【题】比较含退格的字符串

基础栈操作,无难点

class Solution {
public:
    bool backspaceCompare(string S, string T) {
        stack<char> s,t;

        int n=S.size();
        for(int i=0;i<n;i++){
            if(S[i]=='#' && !s.empty()) s.pop();
            else if(S[i]!='#') s.push(S[i]);
        }

        n=T.size();
        for(int i=0;i<n;i++){
            if(T[i]=='#' && !t.empty()) t.pop();
            else if(T[i]!='#') t.push(T[i]);
        }

        while(!s.empty() && !t.empty()){
            if(s.top()==t.top()){
                s.pop();
                t.pop();
            }
            else return false;
        }

        if(!s.empty() || !t.empty()) return false;
        return true;
    }
};
官方精简版解法

直接操作字符串

class Solution {
public:
    bool backspaceCompare(string S, string T) {
        return build(S) == build(T);
    }

    string build(string str) {
        string ret;
        for (char ch : str) {
            if (ch != '#') {
                ret.push_back(ch);
            } else if (!ret.empty()) {
                ret.pop_back();
            }
        }
        return ret;
    }
};
官方还有一个双指针法,从末尾开始比较

在这里插入图片描述
在这里插入图片描述

五、STL:string

c++中的string常用函数用法总结

用法总结不全,有时间自己弄一个

注意
push_back() 用于在字符串末尾添加一个字符(必须是使用单引号表明的字符)
pop_back() 用于删除字符串的最后一个字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值