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");//直接将字符串替换      }
}

相关内容

热门资讯

遇到交通事故纠纷如何选择适配的... 道路交通相关纠纷的常见法律痛点 随着城市机动车保有量持续攀升,道路通行场景下的各类纠纷发生率也随之增...
沙特消息称伊朗提出有条件地将浓... 5月19日消息,据沙特方面当地时间5月18日消息,伊朗旨在结束中东冲突的最新修订方案包括有条件地将浓...
一季度险资逢低买入,股票持仓继... 5月19日消息,今年一季度,市场波动较大,保险资金是否减仓了股票?答案是“否”。金融监管总局近日公布...
权益类贡献过半,基金年内分红近... 5月19日消息,进入5月,近200只公募基金产品已实施或即将派发红利。年内基金“红包”总额已逼近63...
云深处2025年度营业收入3.... 5月18日消息,杭州云深处科技股份有限公司科创板首次公开发行股票招股说明书显示,公司2025年度营业...
这10张图,让你重新认识生活!... 在这个快节奏的时代,我们常常被各种琐事所困扰,忘记了生活的真谛。今天,让我们通过这10张图,换一种心...
黄岩岛海域发现蓝洞 海洋蓝洞是地球上罕见的地质地貌单元,因其深邃的蓝色水体与独特的洞穴结构成为极具吸引力的自然景观,常被...
今晨两国突发7级左右强震!首都... 北京时间6月25日6时许 委内瑞拉、日本先后发生地震 委内瑞拉7.1级地震!首都震感强烈!美国发布海...
5月1日至15日深圳一手房成交... 5月18日消息,一线城市中,4月份深圳新房价格环比涨幅为0.1%,二手房环比涨幅为0.3%。“五一”...
谷歌大涨2.7%创历史新高,市... 5月18日消息,伯克希尔哈撒韦将Alphabet持仓增至三倍以上后,谷歌(GOOGL.US)股价大涨...