题号20:Invalid Parentheses
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: trueExample 2:
Input: "(]" Output: false
算法
- 初始化栈 S。
- 一次处理表达式的每个括号。
- 如果遇到开括号,我们只需将其推到栈上即可。这意味着我们将稍后处理它,让我们简单地转到前面的 子表达式。
- 如果我们遇到一个闭括号,那么我们检查栈顶的元素。如果栈顶的元素是一个相同类型的左括号,那么我们将它从栈中弹出并继续处理。否则,这意味着表达式无效。
- 如果到最后我们剩下的栈中仍然有元素,那么这意味着表达式无效。
复杂度分析
- 时间复杂度:O(n),因为我们一次只遍历给定的字符串中的一个字符并在栈上进行 O(1) 的推入和弹出操作。
- 空间复杂度:O(n),当我们将所有的开括号都推到栈上时以及在最糟糕的情况下,我们最终要把所有括号推到栈上。例如
((((((((((
。
static const auto _ = []()
{ios::sync_with_stdio(false);cin.tie(nullptr);return nullptr;
}();class Solution {
public:bool isValid(string s) {stack<char> stack;for (int i = 0; i < s.length(); i++){char tmp = s[i];if (tmp == '(' || tmp == '[' || tmp == '{')stack.push(tmp);else{if (stack.empty())return false;char topChar = stack.top();if (topChar == '(' && tmp != ')')return false;if (topChar == '{' && tmp != '}')return false;if (topChar == '[' && tmp != ']')return false;stack.pop();}}return stack.empty();}
};