[leetcode]longest Valid Parentheses(!!)
2013-01-08 06:00
369 查看
class Solution { public: int longestValidParentheses(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int nSize = s.size(); int max = 0; int begin = nSize;// where a part of legal parentheses begin stack<int> stk; for(int i = 0; i< nSize; ++i){ if(s[i] == '('){ stk.push(i); } else if(!stk.empty()){ int tmp = stk.top(); stk.pop(); begin = begin < tmp ? begin : tmp; if(stk.empty()) max = max > i - begin + 1 ? max : i - begin +1; else max = max > i - stk.top()? max : i - stk.top(); } else{ begin = i + 1; } } return max; } };
充分利用了begin 和stk.top这个两个信息!!!
相关文章推荐
- leetcode: Longest Valid Parentheses
- Java [leetcode 32]Longest Valid Parentheses
- [leetcode]Longest Valid Parentheses
- leetcode 32. Longest Valid Parentheses
- LeetCode-32. Longest Valid Parentheses
- 【LeetCode练习题】Longest Valid Parentheses
- LeetCode - 32. Longest Valid Parentheses
- LeetCode032 Longest Valid Parentheses
- LeetCode Longest Valid Parentheses
- LeetCode算法题目:Longest Valid Parentheses
- LeetCode 032 Longest Valid Parentheses
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
- LeetCode | 32. Longest Valid Parentheses——最长有效括号匹配长度
- LeetCode----Longest Valid Parentheses
- LeetCode-Longest Valid Parentheses
- LeetCode (32) Longest Valid Parentheses
- [LeetCode]Longest Valid Parentheses
- LeetCode - 32. Longest Valid Parentheses
- Leetcode: Longest Valid Parentheses
- [leetcode] Longest Valid Parentheses