提交 ac206fce 编写于 作者: L labuladong

多语言版本示例

上级 f1606f32
......@@ -85,6 +85,51 @@ char leftOf(char c) {
![labuladong](../pictures/labuladong.jpg)
[labuladong](https://github.com/labuladong) 提供 C++ 解法代码:
```cpp
bool isValid(string str) {
stack<char> left;
for (char c : str) {
if (c == '(' || c == '{' || c == '[')
left.push(c);
else // 字符 c 是右括号
if (!left.empty() && leftOf(c) == left.top())
left.pop();
else
// 和最近的左括号不匹配
return false;
}
// 是否所有的左括号都被匹配了
return left.empty();
}
char leftOf(char c) {
if (c == '}') return '{';
if (c == ')') return '(';
return '[';
}
```
[张三](any_link_you_want) 提供 Java 代码:
```java
public boolean isValid(String str) {
// ...
}
private char leftOf(char c) {
// ...
}
```
[李四](any_link_you_want) 提供 Python3 代码:
```python
def isValid(str):
# ...
```
[上一篇:如何k个一组反转链表](../高频面试系列/k个一组反转链表.md)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册