This problem can not be used to write back, because there is an input length of 17173, will explode stack. So you have to manually simulate the stack call.
Public classSolution {Private Static classStackFrame { Public Final CharLeft ; Private intnum; PublicStackFrame (CharLeft ) { This. left =Left ; } @Override PublicString toString () {return"StackFrame [left=" + Left + ", num=" + num + "]"; } } Private Static Finalstack<stackframe> stack =NewStack<stackframe>(); Public intlongestvalidparentheses (String s) {stack.clear (); Stack.push (NewStackFrame (' # '))); intMaxnum = 0; for(inti = 0; I < s.length (); ++i) {Charc =S.charat (i); if(Isleft (c)) {Stack.push (NewStackFrame (c)); } Else if(Ispair (Stack.peek (). Left, C)) {StackFrame frame=Stack.pop (); Stack.peek (). Num+ = Frame.num + 2; if(Stack.peek (). num >maxnum) {Maxnum=stack.peek (). Num; } } Else{stack.peek (). Num= 0; if(Stack.size () > 1) { while(Stack.size () > 1) {stack.pop (); } } } } returnMaxnum; } Private BooleanIsleft (Charc) {returnc = = ' (' | | c = = ' [' | | c = = ' {'); } Private BooleanIspair (CharLeftCharRight ) { if(left = = ' (') { returnright = = ') '; } Else if(left = = ' [') { returnright = = '] '; } Else { returnright = = '} '; } }}
"Leetcode" 32. Longest Valid parentheses