Valid Parentheses

 //网上的做法

1
#include <stack> 2 class Solution { 3 public: 4 bool isValid(string s) { 5 6 stack<char> str; 7 for(char&c:s) //将S的每个字符依次赋值到C里面 8 { 9 switch(c) 10 { 11 case '(': 12 case '[': 13 case '{':str.push(c); 14 break; 15 case ')':if(str.empty()||str.top()!='(')return false; 16 else str.pop(); 17 break; 18 case ']':if(str.empty()||str.top()!='[')return false; 19 else str.pop(); 20 break; 21 case '}':if(str.empty()||str.top()!='{')return false; 22 else str.pop(); 23 break; 24 default:; 25 } 26 } 27 return str.empty(); 28 } 29 };

 

posted on 2015-10-21 16:12  RenewDo  阅读(116)  评论(0编辑  收藏  举报

导航