leetcode_020_Valid_Parentheses
给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: “()”
输出: true
示例 2:
输入: “()[]{}”
输出: true
示例 3:
输入: “(]”
输出: false
示例 4:
输入: “([)]”
输出: false
示例 5:
输入: “{[]}”
输出: true
解决方案
算法
1.初始化栈 S。
2.一次处理表达式的每个括号。
3.如果遇到开括号,我们只需将其推到栈上即可。这意味着我们将稍后处理它,让我们简单地转到前面的子表达式。
4.如果我们遇到一个闭括号,那么我们检查栈顶的元素。如果栈顶的元素是一个相同类型的左括号,那么我们将它从栈中弹出并继续处理。否则,这意味着表达式无效。
5.如果到最后我们剩下的栈中仍然有元素,那么这意味着表达式无效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { private HashMap<Character,Character> mappings; public Solution(){ this.mappings = new HashMap<Character,Character>(); this.mappings.put(')','('); this.mappings.put('}','{'); this.mappings.put(']','['); } public boolean isValid(String s) { Stack<Character> stack = new Stack<Character>(); for(int i = 0;i < s.length();i++){ char c = s.charAt(i); if(this.mappings.containsKey(c)){ char topElement = stack.empty() ? '#' : stack.pop(); if(topElement != this.mappings.get(c)){ return false; } }else{ stack.push(c); } } return stack.isEmpty(); } }
|
复杂度分析
- 时间复杂度:O(n),因为我们一次只遍历给定的字符串中的一个字符并在栈上进行O(1)的推入和弹出操作。
- 空间复杂度:O(n),当我们将所有的开括号都推到栈上时以及在最糟糕的情况下,我们最终要把所有括号推到栈上。例如 ((((((((((。