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

相关内容

热门资讯

马斯克自建全球最大芯片厂 马斯... 美国当地时间3月21日,特斯拉宣布将与SpaceX、xAI合作建设芯片项目TeraFab,目标是实现...
美国10年期国债收益率短线跳水... 2月21日消息,美国10年期国债收益率短线跳水大约3个基点,跌穿4.46%刷新日低,日内整体跌超4个...
国常会:在电信、教育、文化、医... 2月21日消息,国务院总理李强主持召开国务院常务会议,会议指出,发展服务贸易和服务消费是稳外贸、扩内...
晶澳科技:筹划发行H股股票并在... 2月21日消息,晶澳科技公告称,为推动全球化发展战略,公司计划发行境外上市外资股(H股)股票并申请在...
阳光集团冲A“卡壳”逾两年半 ... 自1998年成立以来,山西阳光焦化集团股份有限公司(以下简称“阳光集团”)已在薛靛民的带领下走过近3...
摩根大通阿里投资者会:公司长期... 尽管阿里巴巴业绩发布后召开的投资者会议强化了其长期投资叙事,摩根大通认为公司股价短期内仍将受制于盈利...
AI自主决策权扩大,Anthr... Anthropic正在赋予其AI编程工具更大的自主权,同时试图在效率与安全之间寻求平衡。3月24日,...
美国正与多方讨论美伊和谈可能性... 当地时间3月24日消息,美国正与多个斡旋方讨论最快于本周举行对伊朗的高层和平会谈,但仍在等待德黑兰方...
同比大增逾500% 港股今年以... 财联社3月25日电,随着2只新股3月24日登陆港股,港股上市公司规模再度扩容。Wind数据显示,20...
“扛不住”内存涨价潮:小米手机... 本文来源:时代财经 作者:庞宇 张照 图源:视觉中国3月24日晚,小米集团(01810.HK)发布...