标签:style blog http color os io 2014 art
解法:用栈,注意栈的用法和switch的用法
1 class Solution { 2 public: 3 bool isValid(string s) { 4 5 stack<char> istack; 6 string::iterator p=s.begin(); 7 8 while(p!=s.end()){ 9 switch(*p) 10 { 11 case ‘(‘: 12 istack.push(‘(‘); 13 break; 14 case ‘[‘: 15 istack.push(‘[‘); 16 break; 17 case ‘{‘: 18 istack.push(‘{‘); 19 break; 20 case ‘)‘: 21 if(!istack.empty() && istack.top()==‘(‘) 22 istack.pop(); 23 else 24 return false; 25 break; 26 case ‘]‘: 27 if(!istack.empty() && istack.top()==‘[‘) 28 istack.pop(); 29 else 30 return false; 31 break; 32 case ‘}‘: 33 if(!istack.empty() && istack.top()==‘{‘) 34 istack.pop(); 35 else 36 return false; 37 break; 38 default: 39 break; 40 } 41 p++; 42 } 43 if(istack.empty()) 44 return true; 45 else 46 return false; 47 } 48 };
LeetCode : valid parentheses,布布扣,bubuko.com
标签:style blog http color os io 2014 art
原文地址:http://www.cnblogs.com/crystal-miao/p/3879993.html