package 美团笔试; import java.util.Stack; public class Main { public static void main(String[] args) { String s="((()))()(()())()((()))"; Stack<Integer> stack=new Stack<Integer>(); for(int i=0;i<s.length();i++){ char ch=s.charAt(i); if(ch=='(') stack.push(-1); else if(ch==')'){ if(stack.peek()==-1){ stack.pop(); stack.push(2); }else { int tmp=1; while(!stack.isEmpty()&&stack.peek()!=-1){ tmp*=stack.pop()%1000000007; } tmp=tmp+1; stack.pop(); stack.push(tmp); } } } int ans=1; while(!stack.isEmpty()){ ans*=stack.pop()%1000000007; } System.out.println(ans); } }
哎 笔试的时候脑袋抽了,,,,,好气 。。。。。。
全部评论
(7) 回帖