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

相关内容

热门资讯

为何投资者会轻信伪财经大V荐股... “伪财经大V”荐股骗局系列调查 第3期从证监会罚没超8300万元的重磅罚单,到雪球、抖音等平台掀起专...
“00后”上任A股公司董事长、... 2月7日,鸿铭股份(SZ301105,市值26.37亿元)发布关于选举公司第四届董事会董事长、各专门...
8部门“攥指成拳”强监管,合力... 刷短视频弹出“投资稳定币保本保息”的诱人广告,亲友群里转发“海外理财捷径”,朋友圈看到“资产代币化能...
仓库里存着4吨黄金,却硬说没钱... 一家百年珠宝企业,身怀价值数十亿的黄金库存,却因一笔相对不大的债务陷入违约漩涡,宁愿承受司法冻结、监...
突发!刘强东拿下中兴 订阅 快刀财经 ▲ 做您的私人商学院这场仗,刘强东输不起。作者:李东阳来源:李东阳朋友圈(ID:LD...
AI之争成了“外卖大战”,千问... “一代人有一代人的鸡蛋要领。”2月6日上午,下载千问并用1分钱买到一杯奶茶后,陈平(化名)和同事们分...
萃华珠宝的股票将于2月9日停牌... 新京报贝壳财经讯 2月7日,萃华珠宝发布公告称,因资金紧张,流动性短缺,公司及子公司多笔金融机构贷款...
5年耗资74亿元吞下四川大型锂... 木绒锂矿。图源:盛新锂能官网本报(chinatimes.net.cn)记者胡雅文 北京报道木绒锂矿采...
新款理想L9亮相,李想需要复制... 出品|虎嗅汽车组作者|王亚骏头图|视觉中国理想的新武器亮相了。2月6日,理想汽车CEO李想在微博上发...
新商业周报 | 美团收购叮咚买... 《CBNData新商业周报》精选本周新商业领域最新动态,公司头条、消费风向、智能创新、营销动态、可持...