关于括号匹配问题,提到这个问题就会想到数据结构栈,关于括号匹配大家也都是熟悉不过了,不过今天刷起这道题,还是让我花了一些时间去回忆这里面的一些细节,尤其是用python去复现的时候。
首先看一下问题描述:
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()" Output: true
Example 2:
Input: "()[]{}" Output: true
Example 3:
Input: "(]" Output: false