猿辅导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); } }
#猿辅导笔试讨论##猿辅导##笔经#