This is the 21st day of my participation in the August Text Challenge.More challenges in August
May oneself also did not think, can be more continuous 21 days. Twenty-one should be a special number, as it was widely believed that it takes 21 days to start a new habit. A more direct evidence is that there was a period of time a few years ago, many technical books would take a title “21 days from Beginner to Master XXX”, of course, later this was also played into a meme, now should appear “21 days from Beginner to Quit XXX”. But in my opinion, forming a good habit is not a matter of days, but of determination and perseverance: it takes determination to keep going at the beginning, and perseverance to give up. It is easy to say, and few people can really do it. However, 21 days is still a small milestone, but crossing today is a new beginning, and all it takes is determination and perseverance.
So let’s get back to problem 32 for Leetcode today.
The title
Given a string containing only ‘(‘ and ‘)’, find the length of the longest valid (well-formed and continuous) parenthesis substring.
Example 1: Input: s = “(()” Output: 2 Explanation: The longest valid parenthesis substring is “()”
Example 2: Input: s = “)()())” Output: 4 Explanation: The longest valid parenthesis substring is “()()”
Example 3: Input: s = “” Output: 0
Train of thought
Although I knew it was a dynamic programming topic at the beginning, IT took me a long time to figure it out. Later, WHEN I read others’ solutions, IT was actually a small point that I didn’t understand, but after I understood it, I suddenly felt very good.
Len = len; len = len; len = len; len = len; len = len The obvious case is that if the subscript I is ‘(‘, that can’t be the end of any legal parenthesis string, so in this case, len [I] = 0.
Discuss next subscript I of the characters in the case of the ‘) ‘consider the 2nd characters at this moment, if the 2nd characters for’ (‘, and then just like the last one character of the parentheses effectively, at least at this time longest [I] = 2, if there are in front of the characters, will be combined with the third characters as of the end of the longest valid parentheses, The longest [I] + = longest [I – 2).
What if the second-to-last character is ‘)’? If (j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j, j Longest [I – 1 – the longest [I – 1]].
In this case, f[] is used to represent longest[] for the convenience of drawing:
Java version code
class Solution { public int longestValidParentheses(String s) { int len = s.length(); if (len == 0) { return 0; Int [] len = len; len = len; len = len; len = len; longest[0] = 0; for (int i = 1; i < len; i++) { if ('(' == s.charAt(i)) { longest[i] = 0; } else {if ('(' == s.char (i-1)) {if ('(' == s.char (i-1)); / / if there are legitimate parentheses, before joining together in front of the if (I > = 2) {longest [I] + = longest [I - 2); If (I == 1) {if (I == 1) {if (I == 1) {if (I == 1) {if (I == 1) { } else { int pre = i - 1 - longest[i - 1]; if (pre >= 0) { if ('(' == s.charAt(pre)) { longest[i] = longest[i - 1] + 2; If (pre-1 >= 0) {if (pre-1 >= 0) {longest[I] += longest[pre-1]; } } else { longest[i] = 0; If (j = 0, j = 0, j = 0, j = 0, j = 0, j = 0, j = 0) } } } } } int ans = 0; for (int i = 0; i < len; i++) { if (longest[i] > ans) { ans = longest[i]; } } return ans; }}Copy the code