27 lines
906 B
Markdown
27 lines
906 B
Markdown
## 题目地址
|
||
https://leetcode-cn.com/problems/valid-parentheses/
|
||
|
||
## 思路
|
||
|
||
括号匹配是使用栈解决的经典问题
|
||
|
||
## C++代码
|
||
|
||
```
|
||
class Solution {
|
||
public:
|
||
bool isValid(string s) {
|
||
stack<int> st;
|
||
for (int i = 0; i < s.size(); i++) {
|
||
if (s[i] == '(') st.push(')');
|
||
else if (s[i] == '{') st.push('}');
|
||
else if (s[i] == '[') st.push(']');
|
||
else if (st.empty() || st.top() != s[i]) return false;
|
||
else st.pop();
|
||
}
|
||
return st.empty();
|
||
}
|
||
};
|
||
```
|
||
> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|