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

相关内容

热门资讯

这家美国云服务供应商斩获英伟达... CoreWeave。视觉中国 资料图美国云服务供应商CoreWeave获得来自AI(人工智能)芯片龙...
V观财报|锦棉种业未按规定履行...   中新经纬9月16日电 据新疆证监局网站16日消息,新疆证监局对新疆锦棉种业科技股份有限公司(下称...
10天9板!7次风险提示难挡宇... 9月16日,首开股份(600376.SH)再度涨停,录得10个交易日内的第9个涨停板,截至午盘报6....
65岁阿姨一顿饭把全家送进医院... 8月5日至9月16日,我国东海、黄渤海、南海海域将先后进入“开渔季”,千帆竞发向海逐“鲜”,一大批海...
8725万!华夏银行,快被罚懵... 作者|杆姐来源|杠杆游戏(ID:zhangyinyin0903)一张8725万元的天价罚单,让华夏银...
继海底捞“科目三”,一曲“珍珠... 总第4340期作者 |餐饮老板内参内参君继海底捞“科目三”一曲“珍珠摇”火爆餐饮圈前段时间,内参君非...
V观财报|天坛生物:云南项目正... 【V观财报|天坛生物:云南项目正在进行检查缺陷整改】天坛生物16日举行2025年半年度业绩说明会,对...
特斯拉Optimus引领热潮,... 9月16日,人形机器人板块开盘拉升,均胜电子快速封住涨停。截至中午收盘,汉威科技(300007.SZ...
10年6000万咨询费!“制造... 9月15日,罗永浩表示“华与华”的老板跟他道歉了,事情就过去了。不过,网友没打算放过“华与华”,他们...
寒武纪午后一度收复1500元关... 9月16日午后,寒武纪一度涨近6%,股价重回1500元上方,现涨幅收窄至近3%,成交额超140亿元。