用Javascript堆栈

时间:2014-04-29 00:55:30

标签: javascript jquery stack

我正在臭名昭着的'Braces'问题,我停了下来。这应该是一个简单的解决方案,但目前我的眼睛并没有多大的帮助。

对该功能的第一次调用就像一个魅力,但第二个是额外的时间,我不明白为什么。

第一个调用返回01110,这是正确的,但第二个调用返回011110,这不是......

如果您无法在此处阅读代码,请转到fiddle

//constructor function for our Stack class
function Stack() {
    this.dataStore = [];
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
    this.length = length;
    this.clear = clear;
}

function push(element) {
    this.dataStore[this.top++] = element;
}

function pop() {
    return this.dataStore[--this.top];
}

function peek() {
    return this.dataStore[this.top - 1];
}

function length() {
    return this.top;
}

function clear() {
    this.top = 0;
}


function braces(expression) {

    for (var i = 0; i < expression.length; i++) {
        //if the number of elements in the expression is odd, it is guaranteed not to have a matching expression
        //therefore we print 0
        if (expression[i].length%2 !== 0) {
            console.log(0);
        } else {
            var s = new Stack();
            var startPoint = expression[i].charAt(0);

            //if the expression starts with an open brace it means we will not have a matching expression so we print 0
            if (startPoint == '(' || startPoint == '{' || startPoint == '[') {
                for (var j = 0; j < expression[i].length; j++) {
                    var char = expression[i].charAt(j);
                    var h = '';
                    if (char == '(' || char == '{' || char == '[') {
                        s.push(char);
                    } else {
                        h = s.peek();
                        if (h == "(" && char == ")") {
                            s.pop();
                        } else if (h == "{" && char == "}") {
                            s.pop();
                        } else if (h == "[" && char == "]") {
                            s.pop();
                        }
                    }
                }
            } else {
                console.log(0);
            }

            if (s.length() == 0) {
                console.log(1)
            } else {
                console.log(0);
            }
        }
    }
}

var expr = [ "}()()", "[]({})", "([])", "{()[]}", "([)]" ]; //working
var expr2 = [ "}()(){", "[]({})", "([])", "{()[]}", "([)]" ]; //running an extra time

braces(expr);

2 个答案:

答案 0 :(得分:3)

改变这个:

        else {
            console.log(0);
            continue; //this is new
        }

        if (s.length() == 0) {

如果起始点不是{([并且s的长度为0

,则您的函数将同时记录0和1/0

答案 1 :(得分:1)

您的堆栈功能都超出了Stack()的范围,因此数据可能不会达到您的预期。你可以通过在Stack()函数中放置函数来开始修复它:

function Stack() {
    this.dataStore = [];
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
    this.length = length;
    this.clear = clear;

    this.pop = function () {
        // pop
    }

    this.push = function () {
        // code
    }

    this.peek = function () {
        // code
    }

}

这样,这些方法都可以访问相同的数据。