博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Longest Valid Parentheses
阅读量:5900 次
发布时间:2019-06-19

本文共 3538 字,大约阅读时间需要 11 分钟。

This problem is a nice extension of the Valid Parentheses problem.

There are several ways to solve it. The first idea is also to use a stack. However, in this time, we push the index instead of the character itself into the stack. What indexes do we push? Well, we push the indexes which seperate two valid parentheses.

Let's see an example "()(()" first. In this example, it is clear that the first two are valid and the last two are also valid. Thus the third characters seperate two valid parentheses. We push its index 3 into the stack. Then the longest valid parentheses is either on the left side of it or on the right side of it. For strings that have more than one such indexes, like "())())()", we push all of them into the stack. Then we visit each valid parentheses separated by them and find the longest length. 

How do we obtain these indexes? In fact, you can simply follow the way in Valid Parentheses. Each time a mismatch occurrs, the current index is what we need.

You may run the following code on the above examples to get an understanding of how it works.

1 class Solution { 2 public: 3     int longestValidParentheses (string s) { 4         stack
indexes; 5 for (int i = 0; i < (int)s.length(); i++) { 6 if (s[i] == '(' || indexes.empty() || s[indexes.top()] != '(') 7 indexes.push(i); 8 else indexes.pop(); 9 }10 if (indexes.empty()) return s.length();11 int maxlen = 0, left = 0, right = s.length() - 1;12 while (!indexes.empty()) {13 left = indexes.top();14 indexes.pop();15 maxlen = max(maxlen, right - left);16 right = left - 1;17 }18 maxlen = max(maxlen, left);19 return maxlen;20 }21 };

 Of course, this problem also has a Dynamic Programming solution. Let's define P[i] to be the length of the longest valid parentheses end at i. Then state equations are:

  1. P[i] = 0 if s[i] == '(' (No valid parentheses end with '(');
  2. P[i] = P[i - 2] + 2 if s[i] == ')' and s[i - 1] == '(', for example, s = '()()';
  3. P[i] = P[i - P[i - 1] - 2] + P[i - 1] + 2 if s[i] == ')' and s[i - 1] == ')' and s[i - P[i - 1] - 1] == '(', for example, s = '(())'.

Putting these together, we have the following code.

1 class Solution { 2 public: 3     int longestValidParentheses(string s) { 4         vector
dp(s.length(), 0); 5 int maxlen = 0; 6 for (int i = 1; i < (int)s.length(); i++) { 7 if (s[i] == ')') { 8 if (s[i - 1] == '(') 9 dp[i] = 2 + (i - 2 >= 0 ? dp[i - 2] : 0);10 else if (i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(')11 dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);12 maxlen = max(maxlen, dp[i]);13 }14 }15 return maxlen;16 }17 };

 You may even notice that case 3 above has already includede case 2. So the code can be further shorten to below.

1 class Solution { 2 public: 3     int longestValidParentheses(string s) { 4         vector
dp(s.length(), 0); 5 int maxlen = 0; 6 for (int i = 1; i < (int)s.length(); i++) { 7 if (s[i] == ')' && i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(') { 8 dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0); 9 maxlen = max(maxlen, dp[i]);10 }11 }12 return maxlen;13 }14 };

 

转载地址:http://cwrsx.baihongyu.com/

你可能感兴趣的文章
JLNotebookView
查看>>
StackPanel
查看>>
SPUserResizableView
查看>>
UML类图示例
查看>>
sh ./ 执行区别
查看>>
宏定义(#ifndef+#define+#endif)的作用
查看>>
关于 HTTP GET/POST 请求参数长度最大值的一个理解误区
查看>>
Prometheus安装部署以及配置
查看>>
Oracle存储过程大冒险-2存储过程常用语法
查看>>
taobao-pamirs-schedule-2.0源码分析——类设计
查看>>
10位程序员眼中的2007:寻找软件开…
查看>>
Stream API
查看>>
Web开发之-DOM操作对象
查看>>
Git 的使用
查看>>
APUE第15章学习扎记之程序的存储区布局试验
查看>>
ubuntu升级16.04 inter idea 中文输入法无效
查看>>
查找命令集:which/whereis/locate/find
查看>>
三目运算判断jsp脚本里面的值
查看>>
Android窗口管理服务WindowManagerService对输入法窗口(Input Method Window)的管理分析...
查看>>
制作img镜像文件的5种方法 .
查看>>