首页 技术 正文
技术 2022年11月12日
0 收藏 650 点赞 4,878 浏览 2793 个字

题目链接:https://leetcode.com/problems/longest-valid-parentheses/description/

题目大意:找出最长的括号匹配的子串长度。例子:”(()(“长度是2;”()()(())”长度是8

解法一:利用三层for循环,逐一的找每一个子串,并判断每一个子串是否是括号匹配的。很遗憾,超时了。代码如下:

     public int longestValidParentheses(String s) {
int res = 0;
for(int i = 0; i < s.length(); i++) {//逐一查看每一个子串
for(int j = i + 2; j <= s.length(); j += 2) {
int cnt = parentheses(s.substring(i, j));
if(res < cnt) {
res = cnt;
}
}
}
return res;
}
//判断是否是括号匹配的
public static int parentheses(String s) {
Stack<Character> st = new Stack<Character>();
int cnt = 0;
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == ')') {
if(!st.isEmpty() && st.peek() == '(') {
cnt++;
st.pop();
}
else {
return 0;
}
}
else {
st.push(c);
}
}
if(!st.isEmpty()) {
return 0;
}
return cnt * 2;
}

解法二(借鉴):用栈存储左括号下标,而不是'(‘左括号。如果是'(‘,则将下标压栈。如果是’)’,则查看栈的情况,如果栈空,则记录下一个子串开始的位置下标(即i+1);如果非空,则查看栈中元素情况,如果只有一个'(‘,则弹出计数子串长度,如果有多个'(‘,则计数到目前为止的匹配的子串长度情况。或者同时压栈存储左括号和右括号下标,当遇到’)’,则查看栈顶元素,如果是'(‘,则计数,否则压栈。这两种方法都是o(n)的时间复杂度。代码如下(耗时26ms):

第一种压栈左括号下标的方法:

 public int longestValidParentheses(String s) {
Stack<Integer> st = new Stack<Integer>();//存'('下标
int res = 0, lastIndex = 0, length = s.length();
for(int i = 0; i < length; i++) {
char c = s.charAt(i);
if(c == '(') {//如果是'(',将下标压栈
st.push(i);
}
else {//如果是')',分情况讨论
if(st.isEmpty()) {//如果为空,则出现')'没有'('匹配的情况,则当前子串结束,下一个子串的开始位置即是当前子串结束的下一个位置
lastIndex = i + 1;
}
else {//如果非空,可能出现两种情况:'()'或'(())'
st.pop();
if(st.isEmpty()) {//如果为空,则说明栈中没有'('需要匹配
res = Math.max(res, i - lastIndex + 1);
}
else {//如果非空,则当前栈中还有'('存在
res = Math.max(res, i - st.peek());
}
}
}
}
return res;
}

第二种压栈左括号和右括号下标的方法(基本与上面第一种一样):

     public int longestValidParentheses(String s) {
Stack<Integer> st = new Stack<Integer>();
int res = 0, length = s.length();
for(int i = 0; i < length; i++) {
if(s.charAt(i) == '(') {//左括号压栈下标
st.push(i);
}
else {//遇到右括号
if(st.isEmpty()) {//如果栈空,则压栈右括号下标
st.push(i);
}
else {
if(s.charAt(st.peek())== '(') {//如果栈顶元素是左括号,则匹配,退栈计数子串长度
st.pop();
res = Math.max(res, (i - (st.isEmpty() ? -1 : st.peek())));
}
else {//如果栈顶元素是右括号,则压栈右括号下标
st.push(i);
}
}
}
}
return res;
}

解法三:利用dp。首先dp[i] 表示从s[i]往前推最长能匹配的子串,换句话说,就是到s[i]为止的最长匹配子串后缀。那么当对于下面几种情况进行分析:

1. s[i] ==’(’     s[i]为左括号,dp[i]=0这个很好理解。
2. s[i] ==’)’  这就要分情况了
  a) 如果s[i-1]是’(’说明已经完成了一次匹配,子串长度为2,但是还要加上dp[i-2]的大小,也就是当前匹配的这对括号前面的最长匹配长度,它们是相连的。
  b) 如果s[i-1]是’)’这样不能匹配,则需要考虑s[i-1-dp[i-1]]的情况了,如果s[i-1-dp[i-1]]是一个左括号,则与当前右括号匹配,那么此时我们令dp[i]=dp[i-1]+2,这个2就是刚刚匹配的左右括号。最后还要把dp[i-2-dp[i-1]](即与当前括号’)’匹配的'(‘前面一个位置的dp长度,它们是相连的)值加起来,把相连的最大长度求出来。代码如下(耗时20ms):

     public int longestValidParentheses(String s) {
int length = s.length();
int[] dp = new int[length];
int res = 0;
for(int i = 0; i < length; i++) {
dp[i] = 0;
if(s.charAt(i) == ')' && (i - 1) >= 0) {
if((i - 1) >= 0 && s.charAt(i - 1) == '(') {//如果前一个位置与当前括号')'匹配
dp[i] = 2;//暂且只计算匹配的'('')'
if(i - 2 >= 0) {//加上与')'匹配的'('前一个位置的dp匹配长度
dp[i] += dp[i - 2];
}
}
else {//如果前一个位置与当前括号'('不匹配
if((i - 1 - dp[i - 1]) >= 0 && s.charAt(i - 1 - dp[i - 1]) == '(') {//查看【前一个位置下标-匹配数】之后的字符与当前括号')'是否匹配
dp[i] = dp[i - 1] + 2;//如果匹配,则在前一个位置匹配数的情况下+2,即加上刚与当前')'匹配的左右括号数量
if(i - 2 - dp[i - 1] >= 0) {//加上与')'匹配的'('前一个位置的dp匹配长度
dp[i] += dp[i - 2 - dp[i - 1]];
}
}
}
}
res = Math.max(res, dp[i]);
}
return res;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291