原题地址

 

辅助栈

 

代码:

 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 }

 

相关文章:

  • 2021-06-22
  • 2021-10-03
  • 2022-01-30
  • 2021-12-22
  • 2021-09-26
  • 2021-05-29
猜你喜欢
  • 2021-04-07
  • 2021-07-07
  • 2021-09-08
  • 2021-08-11
  • 2021-12-25
  • 2021-08-13
  • 2022-01-04
相关资源
相似解决方案