leetcode 20 简单括号匹配


栈的运用

 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 };

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM