js使用栈或者数组完成LeetCode算法题第20道(有效的括号)

文章结构

leetCode20 有效的括号

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
<h1>题目描述</h1>
<h4>
    给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。
    <a href="https://leetcode-cn.com/problems/valid-parentheses" target="_blank">来源:力扣(LeetCode)链接:</a> 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
</h4>
<h1>解题思路</h1>
<h4>遍历字符串,遇到左括号就入栈,遇到右括号就判断是否是栈顶的匹配括号,如果是,就将栈顶出栈,遍历完之后判断栈的长度是否为0,不为0则说明有对应的左括号没有与之对应的右括号</h4>
<h1>尝试使用</h1>
请输入字符串(只能包含'(',')','{','}','[',']' 6种字符):
<input type="text" id="input" />
<br />
<button id="btn">计算</button>
<br /> 是否符合要求
<input type="text" id="output" />
<br />
<h1>代码实现</h1>
<h4>数组的方式</h4>
<pre>
    <code>
        // 数组的方式
        const main = function(s) {
            const array = [];
            for (let i of s) {
                if (i === "(" || i === "{" || i === "[") {
                    array.push(i);
                } else {
                    switch (i) {
                        case ")":
                            if (array[array.length - 1] === "(") {
                                array.pop();
                            } else {
                                return false;
                            }
                            break;
                        case "}":
                            if (array[array.length - 1] === "{") {
                                array.pop();
                            } else {
                                return false;
                            }
                            break;
                        case "]":
                            if (array[array.length - 1] === "[") {
                                array.pop();
                            } else {
                                return false;
                            }
                            break;
                    }
                }
            }
            if (array.length !== 0) {
                return false;
            } else {
                return true;
            }
        }
        document.getElementById("btn").addEventListener("click", function() {
            const inputValue = document.getElementById("input").value;
            const outputValue = main(inputValue);
            // 布尔型转字符型
            document.getElementById("output").value = outputValue + "";
        });
    </code>
</pre>
<h4>栈的方式</h4>
<pre>
    <code>
        // 栈的方式
        const main = function(s) {
            const stack = new Stack();
            for (let i of s) {
                if (i === "(" || i === "{" || i === "[") {
                    stack.push(i);
                } else {
                    switch (i) {
                        case ")":
                            if (stack.peek() === "(") {
                                stack.pop();
                            } else {
                                return false;
                            }
                            break;
                        case "}":
                            if (stack.peek() === "{") {
                                stack.pop();
                            } else {
                                return false;
                            }
                            break;
                        case "]":
                            if (stack.peek() === "[") {
                                stack.pop();
                            } else {
                                return false;
                            }
                            break;
                    }
                }
            }
            return stack.isEmpty();
        }
        document.getElementById("btn").addEventListener("click", function() {
            const inputValue = document.getElementById("input").value;
            const outputValue = main(inputValue);
            // 布尔型转字符型
            document.getElementById("output").value = outputValue + "";
        });
    </code>
</pre>

<body>
    <script src="../../Stack.js"></script>
    <script>
        // // 数组的方式
        // const main = function(s) {
        //     const array = [];
        //     for (let i of s) {
        //         if (i === "(" || i === "{" || i === "[") {
        //             array.push(i);
        //         } else {
        //             switch (i) {
        //                 case ")":
        //                     if (array[array.length - 1] === "(") {
        //                         array.pop();
        //                     } else {
        //                         return false;
        //                     }
        //                     break;
        //                 case "}":
        //                     if (array[array.length - 1] === "{") {
        //                         array.pop();
        //                     } else {
        //                         return false;
        //                     }
        //                     break;
        //                 case "]":
        //                     if (array[array.length - 1] === "[") {
        //                         array.pop();
        //                     } else {
        //                         return false;
        //                     }
        //                     break;
        //             }
        //         }
        //     }
        //     if (array.length !== 0) {
        //         return false;
        //     } else {
        //         return true;
        //     }
        // }

        // 栈的方式
        const main = function(s) {
            const stack = new Stack();
            for (let i of s) {
                if (i === "(" || i === "{" || i === "[") {
                    stack.push(i);
                } else {
                    switch (i) {
                        case ")":
                            if (stack.peek() === "(") {
                                stack.pop();
                            } else {
                                return false;
                            }
                            break;
                        case "}":
                            if (stack.peek() === "{") {
                                stack.pop();
                            } else {
                                return false;
                            }
                            break;
                        case "]":
                            if (stack.peek() === "[") {
                                stack.pop();
                            } else {
                                return false;
                            }
                            break;
                    }
                }
            }
            return stack.isEmpty();
        }
        document.getElementById("btn").addEventListener("click", function() {
            const inputValue = document.getElementById("input").value;
            const outputValue = main(inputValue);
            // 布尔型转字符型
            document.getElementById("output").value = outputValue + "";
        });
    </script>
</body>

</html>

Stack.js

// 栈
const Stack = function(item) {
    // 不使用因为const item = [];当使用Stack创建多个栈时,使用的都是这个[]
    // const item = [];

    this.item = item ? item : [];
    // 不使用this.push的原因是因为这样做,每一个new出来的对象都会去开辟一块空间用来保存这个方法,因为this会指向对象
    // 入栈
    this.__proto__.push = function(Element) {
        this.item.push(Element);
    };

    // 出栈
    this.__proto__.pop = function() {
        return this.item.pop();
    };

    // 获取整个栈
    this.__proto__.getStack = function() {
        return this.item;
    };

    // 检查栈顶元素
    this.__proto__.peek = function() {
        return this.item[this.item.length - 1];
    };

    // 检查栈是否为空
    this.__proto__.isEmpty = function() {
        return this.item.length == 0;
    };

    // 清除栈
    this.__proto__.clear = function() {
        this.item = [];
    };

    // 获取栈的长度
    this.__proto__.size = function() {
        return this.item.length;
    };
};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiuJie_Boom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值