mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-09 15:03:54 +08:00
多语言版本示例
This commit is contained in:
BIN
pictures/result.jpg
Normal file
BIN
pictures/result.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
@ -85,6 +85,51 @@ char leftOf(char c) {
|
||||
|
||||

|
||||
|
||||
[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)
|
||||
|
||||
|
Reference in New Issue
Block a user