• 力扣(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),除若干变量使用的常量级空间,没有使用额外的线性空间。
  • 相关阅读:
    从头开始——重新布置渗透测试环境的过程记录(From Windows To Mac)
    SpringMVC相关知识点
    SwaggerSpy:一款针对SwaggerHub的自动化OSINT安全工具
    JVM 垃圾回收
    518. 零钱兑换 II -- 完全背包
    Java面试题之线程通信的方式
    Fabric.js 图案笔刷
    《大数据之路:阿里巴巴大数据实践》-第1篇 数据技术篇 -第6章 数据服务
    spring cloud+spring boot__基于AOP面向切面编程记录操作日志完整流程记录
    分布式缓冲-插槽与集群伸缩
  • 原文地址:https://blog.csdn.net/Innocence02/article/details/127712758