[LeetCode]20_Valid Parentheses
来源:互联网 发布:淘宝闲鱼官方下载 编辑:程序博客网 时间:2024/05/21 17:15
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
括号匹配问题,直接遍历整个字符串,如果遇到左括号则将左括号放入一个栈中,如果遇到右括号则从栈顶取出一个左括号,比对是否是一对,如果不是一对则说明不是标准的括号组形式,最后如果遍历结束栈为空则说明是标准括号组返回true
let isValid = (str)=>{ let left_stack = [] for(let i=0;i<str.length;i++){ if(str[i]==='('||str[i]==='['||str[i]==='{'){ left_stack.push(str[i]) } if(str[i]===')'){ let left_ = left_stack.pop(); if(left_!=='('){ return false } } if(str[i]===']'){ let left_ = left_stack.pop(); if(left_ !== '['){ return false } } if(str[i]==='}'){ let left_ = left_stack.pop(); if(left_!=='{'){ return false } } } if(left_stack.length === 0){ return true }else{ return false } }console.log(isValid('['))console.log(isValid('()[]{}'))console.log(isValid('(]'))console.log(isValid("([)]"))console.log(isValid('(('))console.log(isValid('){'))console.log(isValid('([])'))
阅读全文
0 0
- 【LeetCode】20_Valid Parentheses
- LeetCode 20_Valid Parentheses
- [LeetCode]20_Valid Parentheses
- leetcode 019_Valid Parentheses
- leetcode第一刷_Valid Parentheses
- 20_Valid Parentheses徒手尝试#2(Done)
- 0020_Valid Parentheses
- 【LeetCode】125_Valid Palindrome
- leetcode第一刷_Valid Palindrome
- leetcode第一刷_Valid Number
- leetcode第一刷_Valid Sudoku
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- linux下zookeeper的安装配置
- Android adb.exe程序无法启动的方法
- udev和mdev学习总结
- 直播技术简单介绍之直播协议
- flask模板
- [LeetCode]20_Valid Parentheses
- Scala-模式匹配
- 利用docker搭建spark hadoop workbench
- 一组PHP可逆加密解密算法
- javascript原型链的理解
- Android App插件式换肤实现方案
- Python中文编码问题(字符串前面加'u')
- 文案而已,咋就那么多戏?
- 桥接模式(Bridge Pattern)