新手求救 这道题该用分类讨论还是有特别的算法

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Input

()
Output

True
Input

([)
False

False
需要靠虑算法的复杂度不能够太大

这种问题实现一个栈就好了,取一个字符和栈顶字符比较,相同则弹出栈顶元素,不同则新字符入栈,最后如果栈中没有字符结果就为True,否则False

时间复杂度也就是(N),遍历一般而已