solution.md 4.7 KB
Newer Older
1
# 最长有效括号
F
fix bug  
feilong 已提交
2

3
<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>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12
```cpp

```
每日一练社区's avatar
每日一练社区 已提交
13

14
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20
```cpp

```

## 答案
F
fix bug  
feilong 已提交
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
```cpp
class Solution
{
public:
    int longestValidParentheses(string s)
    {
        int n = s.size();
        if (n == 1)
            return 0;
        if (n == 2)
        {
            if (s[0] == '(' && s[1] == ')')
                return 2;
            else
                return 0;
        }

        vector<int> dp(n, 0);
        int max = 0;
        for (int i = 1; i < n; i++)
        {
            if (s[i] == ')' && s[i - 1] == '(' && i >= 2)
                dp[i] = dp[i - 2] + 2;
            if (s[i] == ')' && s[i - 1] == '(' && i == 1)
                dp[i] = 2;
            if (s[i] == ')' && s[i - 1] == ')' && s[i - dp[i - 1] - 1] == '(')
                dp[i] = dp[i - 1] + 2 + dp[dp[i - 1] - 2];
            if (dp[i] > max)
                max = dp[i];
        }
        return max;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
58

59
### A
F
fix bug  
feilong 已提交
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
```cpp
class Solution
{
public:
    int longestValidParentheses(string s)
    {
        stack<int> stk;
        int invalid = -1;
        int len = 0, max_len = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == '(')
            {
                stk.push(i);
            }
            else
            {
                if (stk.empty())
                {
                    invalid = i;
                }
                else
                {
                    stk.pop();
                    if (stk.empty())
                    {
                        max_len = max(i - invalid, max_len);
                    }
                    else
                    {
                        max_len = max(i - stk.top(), max_len);
                    }
                }
            }
        }
        return max_len;
    }
};
```

### B
F
fix bug  
feilong 已提交
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
```cpp
class Solution
{
public:
    int longestValidParentheses(string s)
    {
        stack<int> left;
        left.push(-1);
        int size = 0, maxSize = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '(')
                left.push(i);
            else
            {
                if (left.size() != 1)
                {
                    left.pop();
                    size = i - left.top();
                    maxSize = max(maxSize, size);
                }
                else
                {
                    left.pop();
                    left.push(i);
                }
            }
        }
        maxSize = max(maxSize, size);
        return maxSize;
    }
};
```

### C
F
fix bug  
feilong 已提交
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
```cpp
class Solution
{
public:
    int longestValidParentheses(string s)
    {
        queue<pair<int, int>> q;
        bool *valid = new bool[s.length() + 1];
        int imax = -1;
        memset(valid, 0, sizeof(bool) * (s.length() + 1));
        for (int i = 0; i < int(s.length()) - 1; i++)
        {
            if (s[i] == '(' && s[i + 1] == ')')
            {
                valid[i] = true;
                valid[i + 1] = true;
                q.push(make_pair(i, i + 1));
            }
        }
        while (!q.empty())
        {
            pair<int, int> parentheses;
            parentheses = q.front();
            q.pop();
            int l = parentheses.first, r = parentheses.second;
            while (l >= 0 && valid[l])
                l--;
            while (r < s.length() && valid[r])
                r++;
            if (!valid[l] && !valid[r])
            {
                if (s[l] == '(' && s[r] == ')')
                {
                    valid[l] = true;
                    valid[r] = true;
                    q.push(make_pair(l, r));
                }
                else
                {
                    l = l + 1;
                    r = r - 1;
                }
            }
            if (r - l > imax)
                imax = r - l;
        }
        return imax + 1;
    }
};

```