Hackerrank超级堆栈代码显示一些错误情况下的超时

时间:2019-05-30 10:49:31

标签: javascript ecmascript-6

Hackerrank超级堆栈代码显示少数错误情况下的超时时间

下面是我正在使用的代码,它在一些测试用例中超时

class SuperStack{
    constructor() {
        this.superStack = new Array();
    }

    push(element) {
        this.superStack.push(element);
    }
    pop() {
        return this.superStack.pop();
    }

    inc(e, k) {
        for (let i = 0; i < e; i++){
            this.superStack[i] += k;
        }
    }

    getTopOfStack() {
        return this.superStack[this.superStack.length - 1];
    }

    printTopOfStack() {
        const topOfStack = this.getTopOfStack();
        if (topOfStack === undefined) {
            console.log('EMPTY');
        } else {
            console.log(topOfStack);
        }
    }

}

function superStack(operations) {
    const ss = new SuperStack()

    operations.forEach(operation => {
        if (operation.startsWith('push')) {
            ss.push(Number(operation.split(' ')[1]))
        } else if (operation.startsWith('pop')) {
            ss.pop()
        } else if (operation.startsWith('inc')) {
            ss.inc(Number(operation.split(' ')[1]), Number(operation.split(' ')[2]))
        } else {
            console.log("Invalid")
        }
        ss.printTopOfStack()
    })


}

上面的代码有什么问题,它花了我们很少的测试用例,但不确定是什么情况

0 个答案:

没有答案