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

3
<p>给定一个只包括 <code>'('</code><code>')'</code><code>'{'</code><code>'}'</code><code>'['</code><code>']'</code> 的字符串 <code>s</code> ,判断字符串是否有效。</p><p>有效字符串需满足:</p><ol>	<li>左括号必须用相同类型的右括号闭合。</li>	<li>左括号必须以正确的顺序闭合。</li></ol><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "()"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "()[]{}"<strong><br />输出:</strong>true</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "(]"<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "([)]"<strong><br />输出:</strong>false</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "{[]}"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= s.length <= 10<sup>4</sup></code></li>	<li><code>s</code> 仅由括号 <code>'()[]{}'</code> 组成</li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<font color="red">错误</font>的选项是?</p>
F
fix bug  
feilong 已提交
5

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

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

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
```cpp
int main()
{
    Solution sol;
    string s = "([)]";
    bool res;

    res = sol.isValid(s);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
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 58 59 60 61 62 63 64 65 66 67 68 69
```cpp
class Solution
{
public:
    bool isValid(string s)
    {
        stack<char> symbols;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '(' || s[i] == '[' || s[i] == '{')
                symbols.push(s[i + 1]);
            else
            {
                if (symbols.size() == 0)
                    return false;

                char match;
                if (s[i] == ')')
                    match = '(';
                else if (s[i] == ']')
                    match = '[';
                else if (s[i] == '}')
                    match = '{';
                char c = symbols.top();
                symbols.pop();

                if (c != match)
                    return false;
            }
        }
        if (symbols.size() != 0)
            return false;

        return true;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
70

71
### A
F
fix bug  
feilong 已提交
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 102 103
```cpp
class Solution
{
public:
    bool isValid(string s)
    {
        if (s.size() % 2 != 0)
            return false;
        if (s.size() == 0)
            return true;

        string temp;
        temp.push_back(s[0]);
        for (int i = 1; i < s.size(); ++i)
        {
            int k = temp.size() - 1;
            if (temp[k] == '(' && s[i] == ')')
                temp.pop_back();
            else if (temp[k] == '[' && s[i] == ']')
                temp.pop_back();
            else if (temp[k] == '{' && s[i] == '}')
                temp.pop_back();
            else
                temp.push_back(s[i]);
        }
        return temp.size() == 0;
    }
};
```

### B
F
fix bug  
feilong 已提交
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
```cpp
class Solution
{
public:
    bool isValid(string s)
    {
        if (s.length() == 0)
        {
            return true;
        }
        if (s.length() % 2 != 0)
        {
            return false;
        }
        stack<char> st;
        for (auto item : s)
        {
            if (item == '(' || item == '[' || item == '{')
                st.push(item);
            if (st.empty())
                return false;
            if (item == ')')
            {
                if (st.top() == '(')
                {
                    st.pop();
                }
                else
                    return false;
            }
            else if (item == ']')
            {
                if (st.top() == '[')
                {
                    st.pop();
                }
                else
                    return false;
            }
            else if (item == '}')
            {
                if (st.top() == '{')
                {
                    st.pop();
                }
                else
                    return false;
            }
        }
        return st.empty();
    }
};
```

### C
F
fix bug  
feilong 已提交
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:
    bool isValid(string s)
    {
        stack<char> data;
        map<char, char> match = {{'{', '}'}, {'[', ']'}, {'(', ')'}};
        for (auto &i : s)
        {
            if (i == '(' || i == '[' || i == '{')
            {
                /* code */
                data.push(i);
            }
            else
            {
                if (data.empty())
                    return false;
                char top = data.top();
                data.pop();
                if (match[top] != i)
                    return false;
            }
        }
        return data.size() == 0;
    }
};
```