# 最长有效括号

<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串，找出最长有效（格式正确且连续）括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = "(()"<strong><br />输出：</strong>2<strong><br />解释：</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = ")()())"<strong><br />输出：</strong>4<strong><br />解释：</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>s = ""<strong><br />输出：</strong>0</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li>	<li><code>s[i]</code> 为 <code>'('</code> 或 <code>')'</code></li></ul></div></div>

以下程序实现了这一功能，请你填补空白处内容：

```python
import pdb
class Solution(object):
	def longestValidParentheses(self, s):
		ls = len(s)
		stack = []
		data = [0] * ls
		for i in range(ls):
			curr = s[i]
			if curr == '(':
				stack.append(i)
			else:
				__________________;
		tep, res = 0, 0
		for t in data:
			if t == 1:
				tep += 1
			else:
				res = max(tep, res)
				tep = 0
		return max(tep, res)
if __name__ == '__main__':
	s = Solution()
	print(s.longestValidParentheses(')()())'))
```

## template

```python
import pdb
class Solution(object):
	def longestValidParentheses(self, s):
		ls = len(s)
		stack = []
		data = [0] * ls
		for i in range(ls):
			curr = s[i]
			if curr == '(':
				stack.append(i)
			else:
				if len(stack) > 0:
					data[i] = 1
					data[stack.pop(-1)] = 1
		tep, res = 0, 0
		for t in data:
			if t == 1:
				tep += 1
			else:
				res = max(tep, res)
				tep = 0
		return max(tep, res)
if __name__ == '__main__':
	s = Solution()
	print(s.longestValidParentheses(')()())'))
```

## 答案

```python
if len(stack) > 0:
	data[i] = 1
	data[stack.pop(-1)] = 1
```

## 选项

### A

```python
if len(stack) > 0:
	data[i] = 1
	data[stack.pop(1)] = 1
```

### B

```python
if len(stack) < 0:
	data[i] = 1
	data[stack.pop(-1)] = 1
```

### C

```python
if len(stack) < 0:
	data[i] = 1
	data[stack.pop(1)] = 1
```