[LeetCode][JavaScript]Valid Parentheses
2015-05-17 16:44
155 查看
Valid Parentheses
Given a string containing just the characters
The brackets must close in the correct order,
https://leetcode.com/problems/valid-parentheses/
水题好愉快。
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.
https://leetcode.com/problems/valid-parentheses/
水题好愉快。
/** * @param {string} s * @return {boolean} */ var isValid = function(s) { var stack = []; function brackets(left, right){ if(left === '(' && right === ')'){ return true; }else if(left === '[' && right === ']'){ return true; }else if(left === '{' && right === '}'){ return true; } return false; } var top = null; for(var i in s){ if(/[\({\[]/.test(s[i])){ stack.push(s[i]); }else{ if(stack.length > 0){ top = stack.pop(); if(!brackets(top, s[i])){ return false; } }else{ return false; } } } if(stack.length !== 0){ return false; } return true; };
相关文章推荐
- [leetcode javascript解题]Valid Parentheses
- [LeetCode][JavaScript]Longest Valid Parentheses
- Leetcode 20. Valid Parentheses - javascript
- leetCode 20. Valid Parentheses 字符串
- leetcode: Valid Parentheses
- leetcode:20 Valid Parentheses-每日编程第三十七题
- leetcode 20. Valid Parentheses ---java
- leetcode 52: valid parentheses
- LeetCode Valid Parentheses
- LeetCode020 Valid Parentheses
- leetcode之20. Valid Parentheses
- [LeetCode] Valid Parentheses、Generate Parentheses、Longest Valid Parentheses
- [leetcode]20. Valid Parentheses(java)
- leetcode20.ValidParentheses
- Longest Valid Parentheses[leetcode] 的三种解法
- leetcode20 Valid Parentheses 有效的括号
- LeetCode_20---Valid Parentheses
- [C语言][LeetCode][20]Valid Parentheses
- leetcode-20. Valid Parentheses
- LeetCode: Valid Parentheses [020]