jquery - Stacks with Javascript -
I am working with a stack on the infamous 'braces' problem and I got a stop. It should be an easy decision, but my eyes do not help at this time.
The first call to the function is working like a charm, but the second is going to an extra time and I '
the first call return 01110 which is correct but the second return is 011110 Which is not ...
If you can not read the code here, then
// Constructor function for our stack class function stack () {this.dataStore = []; This.top = 0; This.push = Push; This.pop = pop; This.peek = peeping; This.length = length; This.clear = clear; } Function Push (element) {this.dataStore [this.top ++] = element; } Function (pop) (return.dataStore [- this.top]; } Peak Peak () {this.dataStore return [this.top - 1]; } Length of function () {return.top; } Function clear () {this.top = 0; } Function braces {var i = 0; i & lt; expression; height} i ++) {// If the number of elements in the expression is weird, then it is a guarantee that there is no matching expression / So we print 0 if (expression [i]. Length% 2! == 0) {console.log (0); } And {var s = new stack (); Var start page = expression [i] .charAt (0); // If the expression starts with an open brace, it means that we will not have any matching expression, so we print 0 (startpoint == '(' || startpoint == '{' || startpoint == '['] {For (var j = 0; j & lt; expression [i] .length; j ++) {var char = expression [i] .charAt (j); var h = ''; if (char == '(' || char = = '{' || four == '['] {S} Peute (four);} and {H = Spec (); if (H == "(" & Amp; ARAC == ")") {S.pop ();} and if (h == "{" & amp; amp;;; four} == "} ") {S.pop ();} else if (h ==" ["& Char =="] ") {s.pop ()}}}}} else {console.log (0);} If (s.length () == 0) {Console.log (1)} and {console.log (0);}}}} Va R expr = ["} () ()", "[] ({}}", "([])," {() []} "," ([]] "]; // working word exprode 2 = ["} () () {", "[] ({})", "([])," {() []} "," ([]] "]; // an additional time The braces are running (expr);
change it:
else {console log in (0); continue; // this is new, if (s.length () == 0)
your function is 0 and 1 / 0 will log both if it is not started {
or
(
or [
and its length was 0
Comments
Post a Comment