LeetCode(String)1678. Goal Parser Interpretation
admin
2024-05-08 22:07:21
0次

1.问题

ou own a Goal Parser that can interpret a string command. The command consists of an alphabet of “G”, “()” and/or “(al)” in some order. The Goal Parser will interpret “G” as the string “G”, “()” as the string “o”, and “(al)” as the string “al”. The interpreted strings are then concatenated in the original order.

Given the string command, return the Goal Parser’s interpretation of command.

Example 1:

Input: command = “G()(al)”
Output: “Goal”
Explanation: The Goal Parser interprets the command as follows:
G -> G
() -> o
(al) -> al
The final concatenated result is “Goal”.

Example 2:

Input: command = “G()()()()(al)”
Output: “Gooooal”

Example 3:

Input: command = “(al)G(al)()()G”
Output: “alGalooG”

Constraints:

1 <= command.length <= 100
command consists of “G”, “()”, and/or “(al)” in some order.

2.解题思路

方法1

1.新建一个StringBuffer
2.将command的数据转换成char
3.遍历数组中的元素,进行判断
4.如果元素等于’G’,添加到 StringBuffer result中
5.如果元素等于’(‘并且等于’)‘,添加到 StringBuffer result中
6.如果元素等于’(‘并且等于’a’,添加到 StringBuffer result中
7.返回result,StringBuffer result转换成String类型

方法2

replace()方法直接将字符串替换

3.代码

代码1

class Solution {public String interpret(String command) {StringBuffer result = new StringBuffer();//1.新建一个StringBufferchar[]str = command.toCharArray();//2.将command的数据转换成charfor (int i = 0; i < str.length; i++) {//3.遍历数组中的元素,进行判断if(str[i]=='G') {//4.如果元素等于'G',添加到 StringBuffer result中result.append("G");}else if(str[i]=='('&& str[i+1]==')'){//5.如果元素等于'('并且等于')',添加到 StringBuffer result中result.append("o");}else if(str[i]=='('&& str[i+1]=='a'){//6.如果元素等于'('并且等于'a',添加到 StringBuffer result中result.append("al");}}return result.toString();//7.返回result,StringBuffer result转换成String类型}
}

以下代码的解题思路相同,只是StringBuffer换成了String,结果result不想要使用toString()转成String类型

class Solution {public String interpret(String command) {String result="";//1.新建一个Stringchar[] str = command.toCharArray();//2.将command的数据转换成charfor(int i=0;i//3.遍历数组中的元素,进行判断if(str[i]=='G'){//4.如果元素等于'G',添加到 String result中result+=str[i];}else if(str[i]=='(' && str[i+1]==')'){//5.如果元素等于'('并且等于')',添加到 String result中result+='o';}else if(str[i]=='(' && str[i+1]=='a'){//6.如果元素等于'('并且等于'a',添加到 String result中result+="al";}}return result;  }
}

代码2

class Solution {public String interpret(String command) {return command.replace("()","o").replace("(al)","al");//直接将字符串替换      }
}

相关内容

热门资讯

一分钟了解!欧宝棋牌透视挂功能... 您好,欧宝棋牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款...
玩家必看分享!凯旋棋牌透视挂功... 玩家必看分享!凯旋棋牌透视挂功能脚本,凯旋棋牌本来有透视挂脚本1、这是跨平台的凯旋棋牌轻量版有透视,...
查到实测!天星透视挂功能app... 查到实测!天星透视挂功能app,天星真是有透视挂app1、进入游戏-大厅左侧-新手福利-激活码辅助透...
重大来袭!大齐山东麻将辅助器功... 重大来袭!大齐山东麻将辅助器功能脚本,大齐山东麻将确实有辅助器脚本1、超多福利:超高返利,海量正版游...
关于!九乐游透视功能脚本,九乐... 关于!九乐游透视功能脚本,九乐游原来真的是有透视脚本1、完成九乐游辅助器v3.3的残局,帮助玩家取得...
玩家必备教程!钱塘十三水透视挂... 玩家必备教程!钱塘十三水透视挂功能插件,钱塘十三水确实存在有透视挂插件一、钱塘十三水可以开透视的定义...
来一盘!领悟棋牌辅助器功能脚本... 来一盘!领悟棋牌辅助器功能脚本,领悟棋牌真是是有辅助器脚本1、每一步都需要思考,不同水平的挑战领悟棋...
重磅来袭!星渔乐开挂功能攻略,... 重磅来袭!星渔乐开挂功能攻略,星渔乐确实真的有开挂攻略1、这是跨平台的星渔乐轻量版有透视,在线的操作...
终于懂了!富友茶苑透视挂功能脚... 终于懂了!富友茶苑透视挂功能脚本,富友茶苑真是存在有透视挂脚本1、玩家可以在富友茶苑线上大神俱乐部对...
玩家必备教程!湘江辅助功能脚本... 玩家必备教程!湘江辅助功能脚本,湘江切实真的是有辅助脚本1、玩家可以在湘江透视最简单三个步骤对游戏的...