最长合法括号的长度

题目:已知一个字符串s,仅有'(' 和 ')'构成,求最长完全配对的子串长度。(就是()配对成功).

比如说:"(()", 最长完全配对的字串长度是"()" ,长度为2。

")()())"丁的最长完全配对的字串长度是"()()" ,长度是4。

解题思路,当时有两种解题思路,第一种是,使用栈,把(的数组中的位置放到栈中,遇到)出栈,并且统计长度,最后检查栈,如果栈是空的,放回当前长度,如果栈不是空的,按照出栈的位置分成两部分,递归调用,把不符合的删除,返回两者之间的最大值。

以上方法不推荐,时间消耗太大了,附上代码,供君加勉

 

 public int longestValidParentheses(String s) {

		Stack<Integer> stack = new Stack<Integer>();
		int result = 0;
		char[] temp = s.toCharArray();
		int i = 0;
		int count = 0;
		while (i < temp.length) {

			if (temp[i] == ')' && !stack.isEmpty()) {
				stack.pop();
				count += 2;
				i++;
				result = count > result ? count : result;
				continue;
			}

			if (temp[i] == '(') {
				stack.push(i);
				i++;
				continue;
			}
			count = 0;
			i++;
		}
		if (!stack.isEmpty()) {
			int k=0;
			while(stack.isEmpty()){
				 k=stack.pop();
			}
			String ns = s.substring(k+1);
			int subresult = longestValidParentheses(ns);
			result = subresult > (result - subresult) ? subresult
					: (result - subresult);

		}
		return result;

	}

 这个方法慢到我自己都无法忍受了,

我要改进,喝了酒,打完dota,突然发现这个其实就是简单的动态规划阿,

下面是第二种方法:

思路是

对S中从后向前的每个 ' ( ' ,我们来计算它最远对应到的那个‘)’的位置

记当前 '(' 的下标为 i ,用match[] 来保存每个i的最右合法匹配位置:

1. 如果它的右边是 ' ) ',那明显最远的合法匹配位置就到这个‘)’为止,match[i] = i+1;

2.如果它的右边是 ' ( ',因为我们已经计算过它的最右合法匹配位置,在接着这个最右位置继续向右找 ;

 

3.如果右边不是 ‘( )’,则没有合法的匹配位置, mathch[i] = -1 ;

例子: 比如  s = " ( ) ( ( ( ) ) "

序号     0    1     2   3   4   5   6   7

             (      )     (    (    (    (     )    )

 

现在来计算match[i]的值;

1.从右向左找到第一个 ‘ ( ’,序号为5,右边是 ‘)’,匹配,所以 match[ 5 ] = 6;

2.第二个s[4] ,右边是'(' ,它的匹配是6,所以跳到6再看右边,是‘)’,匹配,所以 match[ 4 ] = 7;

3. 第三个s[3],右边是 '( " ,它的匹配是7,跳到7再看右边,越界,没有匹配,所以match[3] = -1;

4.第四个s[2] ,右边是‘(’,但右边的‘(‘的match[3]为-1,即没有合法的匹配,所以s[2]自然也不能向右有合法匹配,match[2]=-1;

5. s[0]  = 1;

所以得到match 数组  :

序号             0    1     2   3   4   5    6   7

                  (      )     (     (    (    (     )    )

match[i]       1    -1    -1 -1  7   6  -1   -1

 以下是代码。

	public int longestValidParentheses(String s) {

		char[] tochar = s.toCharArray();
		int[] match = new int[tochar.length];
		int i = s.length() - 1;
		int result = 0;
		while (i > -1) {
			if (tochar[i] == ')')
				match[i] = -1;
			if (tochar[i] == '(') {
				int j = i + 1;

				while (j < tochar.length && j > 0) {
					if (tochar[j] == ')') {
						match[i] = j;
						break;
					}
					if (tochar[j] == '(') {
						j = match[j] + 1;
						continue;
					}
				}
				//判断完成后在加入这个 条件,之前是先加入的,发现以 (结尾就会出错了。
				if (j >= tochar.length||j==0) //j==0 是为了方式最后以(((结尾的情况。
					match[i] = -1;
			}
			i--;
		}
		int curr = 0;
		for (int k = 0; k < match.length;) {
			if (match[k] == -1) {
				result = result > curr ? result : curr;
				k++;
				curr=0;
			} else {
				curr += match[k] - k + 1;
				k = match[k] + 1;
			}
		}
		result = result > curr ? result : curr;

		return result;

	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值