LeetCode刷题day032 (Jieky)

LeetCode第32题 Longest Valid Parentheses

/*
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring 

Example 1: Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2: Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
*/

import java.util.*;
public class LongestValidParentheses{
	public static void main(String[] args){
		String str = "))()))";
		LongestValidParentheses lvp = new LongestValidParentheses();
		int result = lvp.longestValidParentheses(str);
		System.out.println(result);
	}
	
	public int longestValidParentheses(String s) {
		int maxans = 0;
		Stack<Integer> stack = new Stack<>();
		stack.push(-1);
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '(') {
				stack.push(i);
			} else {
				stack.pop();
				if (stack.empty()) {
					// 只有剩下右括号,出栈后变为空,重新入一个右括号,为下次有效字符串计算做准备
					// 这里保证,栈中最多存在一个无效右括号,连续的两个无效右括号出现后,最后一个右括号索引才会保存
					stack.push(i);
				} else {
					// 有效消除字符串后,用当前索引减去栈顶索引 即当前的有效长度
					// 当栈顶为右括号时,可以获得当前局部最长有效字符串
					maxans = Math.max(maxans, i - stack.peek());
				}
			}
		}
		return maxans;
	}

	
	// 动态规划,下一个状态基于上一个状态更新
	public int longestValidParentheses03(String s) {
		int maxans = 0;
		// 首先我们初始化所有的 dp 都等于零。
		int dp[] = new int[s.length()];
		
		// 根据字符串结尾字符进行判断。例如:下标 1 结尾的最长合法字符串长度是 2
		for (int i = 1; i < s.length(); i++) {
			// 受暴力破解有效子串启发,合法子串以右括号结尾
			// 以左括号结尾的字符串一定是非法序列,所以 dp 是零,不用更改。
			if (s.charAt(i) == ')') {
				//右括号前边是左括号
				if (s.charAt(i - 1) == '(') {
					// dp[1]做特殊处理,2表示当前字符和匹配字符的长度
					dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
				//右括号前边是右括号,并且除去前边的合法序列的前边是左括号(前提是还剩字符串)
				} else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
					dp[i] = dp[i - 1] + ((i - dp[i - 1]) >= 2 ? dp[i - dp[i - 1] - 2] : 0) + 2;
				}
				maxans = Math.max(maxans, dp[i]);
			}
		}
		return maxans;
	}

	
	public int longestValidParentheses02(String s) {
		int count = 0;
		int max = 0;
		for (int i = 0; i < s.length(); i++) {
			count = 0;
			for (int j = i; j < s.length(); j++) {
				if (s.charAt(j) == '(') {
					count++;
				} else if (s.charAt(j) == ')'){
					count--;
				}
				
				//count < 0 说明右括号多了,此时无论后边是什么,一定是非法字符串了,所以可以提前结束循环
				if (count < 0) {
					// break跳出最近的一个循环
					break;
				}
				
				//当前是合法序列,更新最长长度
				if (count == 0) {
					if (j - i + 1 > max) {
						max = j - i + 1;
					}
				}
			}
		}
		return max;
	}

	
	public boolean isValid(String s) {
		// 栈,先进后出
		Stack<Character> stack = new Stack<Character>();
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '(') {
				stack.push('(');
			} else if (s.charAt(i) == ')' && !stack.empty() && stack.peek() == '(') {
				stack.pop();
			} else {
				return false;
			}
		}
		return stack.empty();
	}
	public int longestValidParentheses01(String s) {
		int maxlen = 0;
		for (int i = 0; i < s.length(); i++) {
			for (int j = i + 2; j <= s.length(); j+=2) {
				// substring左闭右开
				if (isValid(s.substring(i, j))) {
					maxlen = Math.max(maxlen, j - i);
				}
			}
		}
		return maxlen;
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值