LeetCode: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.

 
 
 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         stack<char> cstack;
 5         string left="({[";
 6         string right=")}]";
 7         
 8         for(auto c:s)
 9         {
10             if(left.find(c)!=s.npos)
11                 cstack.push(c);
12             else if(cstack.empty()||cstack.top()!=left[right.find(c)])
13                 return false;
14             else
15                 cstack.pop();
16         }
17     
18         return cstack.empty();
19         
20     }
21 };

 

posted @ 2015-08-04 11:25  尾巴草  阅读(109)  评论(0编辑  收藏  举报