猿辅导20210731编程题第二道

import java.util.*;
public class Main {
	public static void main(String[] args) {
		String s = "[][][[[]3[]2]2]2";
		int res=0;
		Stack<Integer> st =new Stack<>();
		for(int i=0;i<s.length();i++){
			if(s.charAt(i)=='['){
				st.push(0);
			}
			else if(s.charAt(i)==']'){
				int temp=1;
				while(st.peek()!=0) temp+=st.pop();
				st.pop();
				if(i+1<s.length()&&Character.isDigit(s.charAt(i+1))){
					temp=temp*(s.charAt(i+1)-'0');
					st.push(temp);
				}
				else{
					st.push(temp);
				}
			}
			else continue;
		}
		while(!st.isEmpty()) res+=st.pop();
		System.out.println(res);
	}
}

#猿辅导笔试讨论##猿辅导##笔经#
全部评论

相关推荐

12-19 22:04
武汉大学 Java
点赞 评论 收藏
分享
10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务