Leetcode#20 Valid Parentheses

原题地址

 

辅助栈

 

代码:

 1 bool isValid(string s) {
 2         stack<char> st;
 3         
 4         for (auto c : s) {
 5             if (st.empty())
 6                 st.push(c);
 7             else if (c == ')') {
 8                 if (st.top() != '(')
 9                     return false;
10                 st.pop();
11             }
12             else if (c == ']') {
13                 if (st.top() != '[')
14                     return false;
15                 st.pop();
16             }
17             else if (c == '}') {
18                 if (st.top() != '{')
19                     return false;
20                 st.pop();
21             }
22             else
23                 st.push(c);
24         }
25         
26         return st.empty();
27 }

 

posted @ 2015-02-02 19:21  李舜阳  阅读(152)  评论(0编辑  收藏  举报