码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1678、设计Goal解析器(暴力+栈+replace)


    请你设计一个可以解释字符串 command 的 Goal 解析器 。command 由 "G"、"()" 和/或 "(al)" 按某种顺序组成。Goal 解析器会将 "G" 解释为字符串 "G"、"()" 解释为字符串 "o" ,"(al)" 解释为字符串 "al" 。然后,按原顺序将经解释得到的字符串连接成一个字符串。

    给你字符串 command ,返回 Goal 解析器 对 command 的解释结果。

    示例 1:

    输入:command = "G()(al)"
    输出:"Goal"
    解释:Goal 解析器解释命令的步骤如下所示:
    G -> G
    () -> o
    (al) -> al
    最后连接得到的结果是 "Goal"

    示例 2:

    输入:command = "G()()()()(al)"
    输出:"Gooooal"

    示例 3:

    输入:command = "(al)G(al)()()G"
    输出:"alGalooG"

    一、直接遍历

    1. char * interpret(char * command) {
    2. int len = strlen(command);
    3. char *res = (char *)malloc(sizeof(char) * (len + 1));
    4. int pos = 0;
    5. for (int i = 0; i < len; i++) {
    6. if (command[i] == 'G') {
    7. pos += sprintf(res + pos, "%s", "G");
    8. } else if (command[i] == '(') {
    9. if (command[i + 1] == ')') {
    10. pos += sprintf(res + pos, "%s", "o");
    11. } else {
    12. pos += sprintf(res + pos, "%s", "al");
    13. }
    14. }
    15. }
    16. return res;
    17. }

    二、栈模拟

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

    三、 replace(java)

    replace:查找替换函数

    Java:

    1. class Solution {
    2. public String interpret(String command) {
    3. return command.replace("()","o").replace("(al)","al");
    4. }
    5. }

    C++:

    1. class Solution {
    2. public:
    3. string interpret(string command) {
    4. while(command.find("()")!=-1)
    5. {
    6. command.replace(command.find("()"),2,"o");
    7. }
    8. while(command.find("(al)")!=-1)
    9. {
    10. command.replace(command.find("(al)"),4,"al");
    11. }
    12. return command;
    13. }
    14. };
  • 相关阅读:
    每日五问(java)
    华为云各项指标介绍
    C#开源、功能强大、免费的Windows系统优化工具 - Optimizer
    【原创】技术出身的我,如何做好一个管理者?
    Linux命令行教程:使用head和tail命令快速查看文件的开头和结尾
    一个基于.Net Core遵循Clean Architecture原则开源架构
    快手进军元宇宙:数字人主播拉动“三驾马车”
    React源码分析2-深入理解fiber
    密码正确无法登陆Linux系统
    HTML5期末考核大作业网站——卫生与健康HTML+CSS+JavaScript
  • 原文地址:https://blog.csdn.net/weixin_59179454/article/details/127715791
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号