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

相关内容

热门资讯

债务100%清偿,创三个历史“... 文章来源|不良资产头条、广东高院“这是我国第一宗财产上百亿、第一宗聘请国外中介机构参与、第一宗境外承...
山姆再陷食品质量危机 山姆店必... 选品风波中的山姆又遇品控挑战。近日,湖南长沙一位山姆会员向《国际金融报》记者透露,其于7月中旬购买的...
重大消息宣布!百股涨停,形势一... 作者 | 燕大这两天在热搜上几乎隐身的核弹级消息——雅鲁藏布江水电站开工,官媒层面只有很少的一小段,...
美股开盘:道指开盘涨0.2%,... 7月21日,道指开盘涨0.2%,标普500涨0.2%,纳指涨0.3%。达美乐涨5%,Q2营收超预期。...
康佳大股东股权转让全部完成,华... 7月21日晚间,南都湾财社记者获悉,康佳发布《关于股东权益变动涉及 B 股部分转让完成暨股份无偿划转...
库迪咖啡全球供应链基地超级工厂... 7月20日,库迪咖啡全球供应链基地超级工厂在安徽马鞍山市当涂县正式开工。超级工厂位于库迪咖啡全球供应...
事关海通客户!国泰海通将于8月... 21世纪经济报道记者孙永乐 上海报道国泰海通(601211.SH)合并重组又有新进展!7月21日晚间...
贵价奶油西瓜软烂变质,山姆再陷... 选品风波中的山姆又遇品控挑战。近日,湖南长沙一位山姆会员向《国际金融报》记者透露,其于7月中旬购买的...
券商董事长履新北京国资委主任 ... 近日,第一创业证券董事长吴礼顺履新北京市国资委主任。此前,吴礼顺一直深耕金融与国资管理领域。业内人士...
正荣地产:安永已辞任公司核数师... 新京报贝壳财经讯 7月21日,正荣地产集团有限公司(简称“正荣地产”)披露,安永会计师事务所(简称“...