首页 > 代码库 > Longest Valid Parentheses
Longest Valid Parentheses
难点1,是栈,2是流程
class Solution {public: int longestValidParentheses(string s) { stack<int> mstack; char cbefore; int count = 0; if(s.empty()) return 0; for(int i=0; i<s.size(); i++) { bool ret; CountTotal(mstack,count); if(mstack.empty()) { if(count!=0) { mstack.push(count); mstack.push(‘a‘); count =0; } mstack.push(s[i]); continue; } ret = CheckValidParentheses(mstack.top(),s[i]); if(ret == false) { if(count!=0) { mstack.push(count); mstack.push(‘a‘); count =0; } mstack.push(s[i]); } else { mstack.pop(); count++; mstack.push(count); mstack.push(‘a‘); count =0; } } CountTotal(mstack,count); int maxcount = count; count =0; while(!mstack.empty()) { if(mstack.top() != ‘a‘) { mstack.pop(); if(count > maxcount) maxcount = count; count =0; continue; }; mstack.pop(); count+=mstack.top(); mstack.pop(); } if(count > maxcount) maxcount = count; return maxcount*2; } bool CheckValidParentheses(char a,char b) { if(a == ‘(‘ && b == ‘)‘) return true; return false; } void CountTotal(stack<int>& mstack,int& count) { while(1) { if(mstack.empty()) return; if(mstack.top() != ‘a‘) return; mstack.pop(); count+=mstack.top(); mstack.pop(); } }};
Longest Valid Parentheses
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。