同步操作将从 编程语言算法集/Java 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package DynamicProgramming;import java.util.Scanner;/*** Given a string containing just the characters '(' and ')', find the length of the longest valid* (well-formed) parentheses substring.** @author Libin Yang (https://github.com/yanglbme)* @since 2018年10月5日*/public class LongestValidParentheses {public static int getLongestValidParentheses(String s) {if (s == null || s.length() < 2) {return 0;}char[] chars = s.toCharArray();int n = chars.length;int[] res = new int[n];res[0] = 0;res[1] = chars[1] == ')' && chars[0] == '(' ? 2 : 0;int max = res[1];for (int i = 2; i < n; ++i) {if (chars[i] == ')') {if (chars[i - 1] == '(') {res[i] = res[i - 2] + 2;} else {int index = i - res[i - 1] - 1;if (index >= 0 && chars[index] == '(') {// ()(())res[i] = res[i - 1] + 2 + (index - 1 >= 0 ? res[index - 1] : 0);}}}max = Math.max(max, res[i]);}return max;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (true) {String str = sc.nextLine();if ("quit".equals(str)) {break;}int len = getLongestValidParentheses(str);System.out.println(len);}sc.close();}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。