diff --git a/pictures/result.jpg b/pictures/result.jpg new file mode 100644 index 0000000..459f946 Binary files /dev/null and b/pictures/result.jpg differ diff --git a/高频面试系列/合法括号判定.md b/高频面试系列/合法括号判定.md index 3e3cdce..60526e3 100644 --- a/高频面试系列/合法括号判定.md +++ b/高频面试系列/合法括号判定.md @@ -85,6 +85,51 @@ char leftOf(char c) { ![labuladong](../pictures/labuladong.jpg) +[labuladong](https://github.com/labuladong) 提供 C++ 解法代码: + +```cpp +bool isValid(string str) { + stack 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)