• 力扣(LeetCode)1678. 设计 Goal 解析器(C++)


    栈模拟

    栈模拟 : 遍历 c o m m a n d command command 的所有字符,记作 x x x x x x ′ G ′ 'G' G , a n s + =   ′ G ′ ans+=~'G' ans+= G x x x 不是 ′ ) ′ ')' ) 则压栈 x x x x x x ′ ) ′ ')' ) , 则匹配栈顶的字符,匹配一个字符,弹栈一个,遇到 ′ a ′ 'a' a c n t A cntA cntA++,直到栈顶为 ′ ( ′ '(' ( 停止匹配。如果 c n t A cntA cntA 0 0 0 a n s + =   ′ o ′ ans+=~'o' ans+= o ,否则 a n s + =   ′ a l ′ ans+=~'al' ans+= al

    考虑到昨天的打卡题,一时手痒,想用栈来做今天的题。当然这题直接匹配也很简单,思路和栈模拟大致相似。

    代码展示
    直接匹配
    class Solution {
    public:
        string interpret(string command) {
            string ans="";
            int cntA=0;
            for(auto x:command){
                if('a'==x) cntA++;
                else if(')'==x){
                    if(cntA){
                        ans+="al";
                        cntA=0;
                    }
                    else ans+='o';
                }
                else if('G'==x) ans+=x;
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    栈模拟
    class Solution {
    public:
        string interpret(string command) {
            stack <char> stk;
            //遇到G,ans+=G;遇到),统计当前匹配,其他字符压栈。
            string ans="";
            for(auto x:command){
                int cntA = 0;
                if('G'==x) ans +=x;
                else if(')'==x) {//遇到')'弹栈匹配
                    if(stk.top()=='l') stk.pop();
                    if(stk.top()=='a'){
                        stk.pop();
                        cntA++;
                    }
                    if(stk.top()=='('){
                        stk.pop();//弹出'('
                        if(cntA) ans+="al";
                        else ans+="o";
                    }
                }
                else stk.push(x);//其他字符
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    博主致语

    理解思路很重要!
    欢迎读者在评论区留言,作为日更博主,看到就会回复的。

    AC

    直接匹配
    栈模拟

    复杂度分析
    栈模拟复杂度分析
    1. 时间复杂度: O ( n ) O(n) O(n) n n n c o m m a n d command command 的长度。一次遍历 e x p r e s s i o n expression expression 的时间复杂度是 O ( n ) O(n) O(n)
    2. 空间复杂度: O ( n ) O(n) O(n)。字符栈 s t k stk stk 最多压栈 n n n 个元素,最坏空间复杂度 O ( n ) O(n) O(n)
    直接匹配复杂度分析
    1. 时间复杂度: O ( n ) O(n) O(n), 一次遍历 e x p r e s s i o n expression expression 的时间复杂度是 O ( n ) O(n) O(n)
    2. 空间复杂度: O ( 1 ) O(1) O(1),除若干变量使用的常量级空间,没有使用额外的线性空间。
  • 相关阅读:
    【虹科分享】什么是Redis数据集成(RDI)?
    汇编经典程序——将一个字节数据以十六进制形式显示
    css基本选择器
    JAVA:面向对象++封装+继承【详细】
    实战一次完整的博彩渗透测试
    Spring整合RabbitMQ
    ubuntu22.04 CH340/CH34x 驱动安装
    【Pytorch with fastai】第 16 章 :训练过程
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    Java+JSP+MySQL基于SSM的物流公司物流订单管理系统-计算机毕业设计
  • 原文地址:https://blog.csdn.net/Innocence02/article/details/127712758