栈的运用

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         stack<char>The_Stack;
 5      int i=0;
 6      The_Stack.push('#');
 7      while(i<s.size()) {
 8          if((s[i]==')'&&The_Stack.top()=='(')||(s[i]==']'&&The_Stack.top()=='[')||(s[i]=='}'&&The_Stack.top()=='{')) {
 9              i++;
10              The_Stack.pop();
11          }
12          else  {
13          The_Stack.push(s[i]);
14          i++;
15          }
16      }
17      if(The_Stack.top()=='#')
18      return true;
19      return false;
20     }
21 };

 

相关文章:

  • 2021-08-08
  • 2022-01-24
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
  • 2019-09-27
猜你喜欢
  • 2021-09-09
  • 2022-02-14
  • 2021-08-10
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
相关资源
相似解决方案