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; };

浙公网安备 33010602011771号