关注
public static String test2pro(String str) {
int i=0;
String temp="";
while(i<str.length()&&str.charAt(i)!='(') {
i++;
}
if(i==str.length()) {
temp= str.substring(0);
}else if(str.charAt(i)=='(') {
temp = str.substring(0,i);
String rest = str.substring(i+1,str.length()-1);
i=0;
int count=0;
while(i<rest.length()) {
char t =rest.charAt(i);
if(t=='(') {
count++;
}else if(t==')') {
count--;
}else if(t==',') {
if(count==0)
break;
}
i++;
}
if(i==0) {
temp+=test2pro(rest.substring(i+1,rest.length()));
}else if(i==temp.length()-1) {
temp = test2pro(rest.substring(0,i))+temp;
}else {
temp+=test2pro(rest.substring(i+1,rest.length()));
temp = test2pro(rest.substring(0,i))+temp;
}
}
return temp;
} 这个是我改进的代码,不用去构造二叉树了,直接遍历字符串加递归,应该没什么问题。
查看原帖
点赞 评论
牛客热帖
更多
正在热议
更多
# 这个offer值得去吗? #
32121次浏览 232人参与
# 校招薪资来揭秘 #
960994次浏览 4063人参与
# 在爱玛,骑向未来 #
20485次浏览 395人参与
# 如果春招能重来,我会___ #
29650次浏览 296人参与
# 24秋招避雷总结 #
1020317次浏览 7098人参与
# 你会因为行情,降低找工作标准吗? #
45495次浏览 333人参与
# 机械人还在等华为开奖吗? #
339288次浏览 1652人参与
# 米哈游求职进展汇总 #
688793次浏览 3348人参与
# 华为池子有多大 #
178182次浏览 931人参与
# 26届春招投递记录 #
8741次浏览 72人参与
# 25届网易互娱暑实进度 #
109130次浏览 802人参与
# 通信/硬件求职避坑tips #
172130次浏览 1170人参与
# 记录我的毕业季 #
4814次浏览 120人参与
# 机械人,你的秋招第一份简历被谁挂了 #
268835次浏览 2451人参与
# 远程面试的尴尬瞬间 #
363930次浏览 2062人参与
# 大学最后一个寒假,我想…… #
103335次浏览 846人参与
# 机械求职避坑tips #
103717次浏览 589人参与
# 你认为小厂实习有用吗? #
145101次浏览 763人参与
# 运营商笔面经互助 #
219693次浏览 1833人参与
# 美团秋招笔试 #
216475次浏览 1192人参与
# 网易求职进展汇总 #
213207次浏览 1524人参与
