zoukankan      html  css  js  c++  java
  • leetcode(2)-有效的括号

    题目描述:

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。

    有效字符串需满足:

    左括号必须用相同类型的右括号闭合。
    左括号必须以正确的顺序闭合。
    注意空字符串可被认为是有效字符串。

    示例:

    输入: "()"
    输出: true

    输入: "(]"
    输出: false

    输入: "([)]"
    输出: false

    代码:

    function isValid(s: string): boolean {
        const map = new Map();
        map.set('{','}');
        map.set('[',']');
        map.set('(',')');
        const stack:string[] = [];
        
        for(let i of s){
            const value = map.get(i);
            if (value) {
                stack.push(value);
            }else{
                let top = stack.pop();
                if (i != top) return false
            }
        }
        if (stack.length) {
            return false
        } else {
            return true
        }
    };
  • 相关阅读:
    实例协议分析RFC1483:AAL5和几种常见ADSL接入技术
    2.2.3 Runaround Numbers
    2.2.2 Subset Sums
    2.2.1 Preface Numbering
    Dynamic Programming
    Data Structures
    2.1.5 Hamming Codes
    2.1.4 Healthy Holsteins
    2.1.3 Sorting a Three-Valued Sequence
    2.1.2 Ordered Fractions
  • 原文地址:https://www.cnblogs.com/helloHT/p/13908882.html
Copyright © 2011-2022 走看看