Skip to content

Commit

Permalink
Update 0020. 有效的括号.md
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 9, 2022
1 parent ded8958 commit 1951f56
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Solutions/0020. 有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

## 题目大意

给定一个只包括 `'('``')'``'{'``'}'``'['``']'` 的字符串 `s` ,判断括号是否匹配。
给定一个只包括 `'('``')'``'{'``'}'``'['``']'` 的字符串 `s`

要求:判断括号是否匹配。

## 解题思路

「栈」的经典应用。

因为括号是成对出现的,所以字符串的长度应为偶数,可以直接判断长度为奇数的字符串不匹配。

然后遍历字符串 s。当遇到左括号时,将其入栈,当遇到右括号时,先判断栈顶元素是否是相同类型的左括号,如果是则将左括号出栈,继续向下遍历;如果不是,则不匹配。
然后遍历字符串 `s`。当遇到左括号时,将其入栈,当遇到右括号时,先判断栈顶元素是否是相同类型的左括号,如果是则将左括号出栈,继续向下遍历;如果不是,则不匹配。

最后遍历完,再判断一下栈是否为空,如果栈空,则匹配,如果栈还有元素,则不匹配。

Expand Down

0 comments on commit 1951f56

Please sign in to comment.