关注
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;
} 这个是我改进的代码,不用去构造二叉树了,直接遍历字符串加递归,应该没什么问题。
查看原帖
点赞 评论
相关推荐


点赞 评论 收藏
分享
09-06 12:49
门头沟学院 Java 点赞 评论 收藏
分享
08-27 13:55
南昌航空大学 硬件开发 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 从顶到拉给所有面过的公司评分 #
46258次浏览 297人参与
# 产品薪资爆料 #
131191次浏览 840人参与
# 宣讲会你有哪些意向不到的收获 #
6248次浏览 43人参与
# 签约/解约注意事项 #
723088次浏览 4104人参与
# 职场破冰,你们都聊什么? #
11995次浏览 99人参与
# 小厂实习有必要去吗 #
56692次浏览 286人参与
# 水滴求职进展汇总 #
6416次浏览 32人参与
# 你怎么评价今年的春招? #
131109次浏览 1369人参与
# 机械制造岗投递时间线 #
28275次浏览 372人参与
# 十一假期一定要干的事 #
18354次浏览 145人参与
# 你的国庆怎么过 #
27637次浏览 253人参与
# 为了求职,我做过的疯狂伪装 #
21199次浏览 458人参与
# 晒晒你的中秋福利 #
19080次浏览 137人参与
# 工作压力大怎么缓解 #
107995次浏览 1072人参与
# bilibili求职进展汇总 #
101430次浏览 868人参与
# 聊聊这家公司值得去吗 #
583111次浏览 3823人参与
# 你面试被问到过哪些不会的问题? #
40091次浏览 1094人参与
# 顺丰求职进展汇总 #
56908次浏览 290人参与
# 秋招的嫡长offer #
55151次浏览 457人参与
# 机械笔面试考察这些知识点 #
12738次浏览 96人参与