[LeetCode] Longest Valid Parentheses 解题思路
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.
问题:求最长有效括号子字符串。
解题思路:
第一次做,以为是求整个字符串的有效括号长度是多少,思考了一会用 stack 可以求到,心想好像蛮简单的。扔到 LeetCode 跑了一下,结果错误,才发下原来是求 连续的最长有效括号长度。
重新理解题意后,开始做第二次。
想象括号匹配,就像玩那种 “天天爱消除” 新游戏,相邻两个字符,左边为 '(', 右边为 ')',则匹配成功,匹配成功的字符被替换为 '.'。
通过最多 n/2 次遍历,就可以把 s 中全部有效括号替换为 '.', 然后统计下连续 '.' 个数,就是题目的解,耗时 O(n*n)。实现后,扔到 LeetCode 上面,居然通过了,就是慢了一些。
再思考有没有更快的方法了,联想到第一次做用的 stack,可以借助 stack 一次遍历就将全部有效括号替换为 '.'。
第一步:一次遍历,stack 只存放 '(' 的下标。
当找到一个 '(',则压进 stack ;
当找到一个 ')',则把 stack.top 对于的字符替换为 '.',并弹出 stack.pop()。耗时O(n)。
第二步:求出连续 '.' 的最长长度, 耗时O(n)。
stack<int> sk;
for (int i = ; i < s.size(); i++) {
if (s[i] == ')') {
if (sk.empty()) {
continue;
}else{
int idx = sk.top();
sk.pop();
s[idx] = marked;
s[i] = marked;
}
}else{
// s[i] is '('
sk.push(i);
}
}
int len = ;
int maxL = ;
for (int i = ; i < s.size(); i++) {
if (s[i] == '.') {
len++;
}else{
maxL = max(maxL, len);
len = ;
}
}
maxL = max(maxL, len);
return maxL;
[LeetCode] Longest Valid Parentheses 解题思路的更多相关文章
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- leetcode Longest Valid Parentheses python
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 通过C#去调用C++编写的DLL
这个问题缠了我2个小时才弄出来,其实很简单.当对方提供一个dll给你使用时,你需要去了解这个dll 是由什么语言写的,怎么编译的,看它的编译类型.这样即使在没有头绪时,你可以先尝使用一些比较热门的编译 ...
- 关于jQuery $.isNumeric vs. $.isNaN vs. isNaN
在jQuery中,有几种方式可以判断一个对象是否是数字,或者可否转换为数字. 首先,jQuery.isNaN()在最新版本中已经被移除了(1.7之后),取而代之的是 jQuery.isNumeric ...
- [uiview animation ...] 这个函数有多少没有认识的可能!翻盘效果 上下左右怎么翻都不怕
1.自己还想着怎么3d 变形 让一个视图绕x/y 轴线翻转 就这么一句代码 [UIView transitionWithView:self.startButton duration:0.5 op ...
- 应用程序中小红点设置方法 (ios)
我们的手机上常常会看到软件的右上角出现小红点,上面显示着你未读的消息数.下面是设置小红点的方法. 1.tabBar上按钮的小红点 因为小红点代表你未读的消息数,所以这个小红点上的数据不是凭空 ...
- UITableView编写可以添加,删除,移动的物品栏(一)
效果图 : 点击编辑按钮: 点击添加按钮 ...
- ios专题 - openSSL
1 概述 1.1 产生背景 基 于万维网的电子商务和网上银行等新兴应用,极大地方便了人们的日常生活,受到人们的青睐.由于这些应用都需要在网络上进行在线交易,它们对网络通信的安全 性提出了更高的要求 ...
- Html table 实现Excel多格粘贴
Html table 实现Excel多格粘贴 电商网站的后台总少不了各种繁杂数据的录入,旁边的运营妹子录完第138条商品的时候,终于忍不住转身吼到:为什么后台的录入表不能像Excel那样多行粘贴!!! ...
- 学习protobuf
一.认识Protobuf ref:http://blog.csdn.net/program_think/article/details/4229773摘要:1. protobuf是一个开源项目.2. ...
- Java面向对象程序设计--接口和内部类
1.接口的定义: In the Java programming language, an interface is not a class but staff[0] = ...
- 【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test
Description Given a big integer number, you are required to find out whether it's a prime number. In ...