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