码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode#20 Valid Parentheses

时间:2015-02-02 21:21:59      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

原题地址

 

辅助栈

 

代码:

 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 }

 

Leetcode#20 Valid Parentheses

标签:

原文地址:http://www.cnblogs.com/boring09/p/4268518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!