32-Longest Valid Parentheses
来源:互联网 发布:2016开淘宝店审核流程 编辑:程序博客网 时间:2024/06/05 10:54
题目
Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.
For “(()”, the longest valid parentheses substring is “()”, which has length = 2.
Another example is “)()())”, where the longest valid parentheses substring is “()()”, which has length = 4
分析
这个有点类似 20-Valid Parentheses
但是要找到Longest 匹配串,所以还是有些变动。
思路一:
- 在原来的方法的基础上,添加start 标志最长匹配串开始的index
- 相应的stack里面也是存下标(int)而不是存(char)
思路二:
动态规划
实现
class Solution {public: int longestValidParentheses(string s) { if (s == "") return false; int len = s.length(); //(int) stack<int> st; char c; int curLen = 0, maxLen = 0, start = 0; for (int i = 0; i < len; i++) { c = s[i]; if (c == '(') { st.push(i); } if (c == ')') { if (!st.empty()) { st.pop(); //keypoint maxLen = st.empty() ? max(maxLen, i - start + 1) : max(maxLen, i - st.top()); } else { start = i + 1; } } } return maxLen; }};
阅读全文
0 0
- [32] Longest Valid Parentheses
- 32Longest Valid Parentheses
- 32Longest Valid Parentheses
- 【32】Longest Valid Parentheses
- 32-Longest Valid Parentheses
- 32-longest valid parentheses
- LeetCode(32)Longest Valid Parentheses
- [Leetcode] #32 Longest Valid Parentheses
- [leetccode 32] Longest Valid Parentheses
- [Leetcode] 32 - Longest Valid Parentheses
- LeetCode 32 Longest Valid Parentheses
- Leetcode 32 Longest Valid Parentheses
- leetcode 32 -- Longest Valid Parentheses
- LeetCode---(32)Longest Valid Parentheses
- leetcode 32 : Longest Valid Parentheses
- leetcode 32: Longest Valid Parentheses
- [leetcode 32]Longest Valid Parentheses
- LeetCode-32 Longest Valid Parentheses
- Java集合类详解
- spring cloud微服务框架 第一天
- Android数据库简单操作例子
- js获取触发事件的元素
- Drawable学习之----LevelListDrawable
- 32-Longest Valid Parentheses
- c++2017-6-22结构、联合与枚举
- 判断当前Android版本是否大于某版本
- HTML+js+css实现点击图片弹出上传文件窗口的两种思路
- PHP中使用 JKS(Java Key Store)的方法
- 布道云计算_阿里云MVP已启航
- (1)--MongoDB数据库的基础知识和使用
- Eclipse-复制项目
- 二叉树