leetcode练习:20. Valid Parentheses
Posted 年糕君の勉强笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode练习:20. 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.
var isValid = function(s) { var a = []; var len = s.length; var temp; if(len == 1) { return false; } for(var i=0;i<len;i++) { if(s[i] == ‘(‘ || s[i] == ‘{‘ || s[i] == ‘[‘) { a.unshift(s[i]); } if(s[i] == ‘)‘ ) { temp = a.shift(); if(temp != ‘(‘) return false; } if(s[i] == ‘}‘ ) { temp = a.shift(); if(temp != ‘{‘) return false; } if(s[i] == ‘]‘ ) { temp = a.shift(); if(temp != ‘[‘) return false; } } if(a.length) return false; else return true; };
以上是关于leetcode练习:20. Valid Parentheses的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode - 20. Valid Parentheses
[LeetCode]20. Valid Parentheses
[LeetCode]20 Valid Parentheses 有效的括号