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死4... 当地时间11月8日,巴西地方政府通报,该国南部巴拉那州遭龙卷风袭击,导致至少5人死亡、超过430人受...
官方声明:王硕威同志不是福建舰... 11月8日,中国舰船研究设计中心官方微信公众号发布“声明”: 一、王硕威同志不是福建舰总设计师,也不...
独家 | 魏正勤调任大润发华东... 《商业观察家》获悉,高鑫零售(大润发+欧尚)CEO沈辉11月3日发布一份人事调整令,宣布自2025年...
森马服饰新增质押6650万股股... 新京报贝壳财经讯 11月8日,森马服饰发布公告,大股东邱坚强将其持有的森马服饰6650万股股份进行质...
为了下个十年,蚂蚁集团拼了! ... 本文来源:时代周报 作者:黄宇昆蚂蚁集团迎来关键组织架构升级。11月7日,蚂蚁集团CEO韩歆毅发布全...
“银发科技”扎堆亮相:地板防滑... 红星资本局11月8日消息 第八届进博会上,“银发经济”成为多家展商瞄准的方向。据联合国发布的《世界人...
老将“超期服役”卸任,2267... 作者 | 谢美浴编辑 | 付影来源 | 独角金融苏农银行(603323.SH)迎来重要人事“换血”。...
聚焦进博|“网红菠萝”跨越一万... “以前贝宁的农民只能把菠萝卖到邻国,而在进博会签下订单,能直接让农民收入翻番。”10月1日,当游客度...
燕翔:相似的PPI、不同的RO... 燕翔系方正证券首席经济学家、中国首席经济学家论坛理事核心结论国内PPI自2022年10月进入负增区间...
重温美好回忆:斯洛特谈阿诺德重... 在即将到来的欧冠联赛中,利物浦将迎战皇家马德里,而这场比赛的焦点之一无疑是阿诺德的回归。作为曾经的副...