1. 程式人生 > >leetcode練習:20. Valid Parentheses

leetcode練習:20. Valid Parentheses

nsh true ets str valid ack span brackets ||

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