LeetCode - 20. Valid Parentheses
2017-02-19 20:07
411 查看
题目:
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.
思路与步骤:
如果是前半个括号,直接输入;如果是后半个,则进行匹配。所以遇到两个问题:1. 用什么数据结构来存储括号;
2. 后半个匹配时的逻辑。
问题解决:
由于后半个匹配是要与最后一个输入的来匹配(注意这里的逻辑关系),所以可以直接用list,最经典的做法是用stack;同理第二个问题也得到解决,即读入一个后半括号时,只有与上一个匹配才是匹配。一种巧妙解法:
上面提到的不管是用list还是stack,基本思路是一致的,只是数据结构的选择不一样。还有一种比较巧妙的思路,直接将前半个括号对应的后半个输入stack,后面直接判断是否相等即可。这样可以使代码更加简洁。编程实现:
这个问题的解决又复习了 Java 的 stack.用List,更快:
public class Solution {public boolean isValid(String s) {
//method-1: My own solution with list, faster than stack
char[] c = s.toCharArray();
if(s.length() == 0 || s.length()%2 == 1 || c[0] == ')' || c[0] == ']' || c[0] == '}') return false;
List<Character> list = new ArrayList<Character>();
for(int i=0; i<s.length(); i++){
if(c[i] == '(' || c[i] == '[' || c[i] == '{') list.add(c[i]);
else if(c[i] == ')'){
if(list.get(list.size()-1) == '(') list.remove(list.size()-1);
else return false;
} else if(c[i] == ']'){
if(list.get(list.size()-1) == '[') list.remove(list.size()-1);
else return false;
} else if(c[i] == '}'){
if(list.get(list.size()-1) == '{') list.remove(list.size()-1);
else return false;
}
}
if(list.size()==0) return true;
else return false;
/*//这一段效率不如上面那一段
for(int i=0; i<s.length(); i++){
if(c[i] == '(' || c[i] == '[' || c[i] == '{') list.add(c[i]);
else if(c[i] == ')' && list.get(list.size()-1) == '(') list.remove(list.size()-1);
else if(c[i] == ']' && list.get(list.size()-1) == '[') list.remove(list.size()-1);
else if(c[i] == '}' && list.get(list.size()-1) == '{') list.remove(list.size()-1);
else return false;
}
return list.size()==0 ? true : false;
*/
}
}
用stack:
public class Solution {public boolean isValid(String s) {
//method-2: classic solution with stack
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '[' || c == '{') stack.push(c);
else if(c == ')' && !stack.isEmpty() && stack.peek() == '(') stack.pop();
else if(c == ']' && !stack.isEmpty() && stack.peek() == '[') stack.pop();
else if(c == '}' && !stack.isEmpty() && stack.peek() == '{') stack.pop();
else return false;
}
return stack.isEmpty();
}
}
用stack,很巧妙的一种方法:
public class Solution {public boolean isValid(String s) {
//method-3: an interesting solution with stack
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(') stack.push(')');
else if (c == '{') stack.push('}');
else if (c == '[') stack.push(']');
else if (stack.isEmpty() || stack.pop() != c) return false;
}
return stack.isEmpty();
}
}
相关文章推荐
- LeetCode 20.Valid Parentheses
- LeetCode 20 Valid Parentheses
- leetcode20-Valid Parentheses
- LeetCode 20. Valid Parentheses
- leetcode-20. Valid Parentheses
- LeetCode: 20. Valid Parentheses
- LeetCode 20: Valid Parentheses
- leetcode[20]:Valid Parentheses
- [Leetcode] 20. Valid Parentheses(Stack)
- 【Leetcode】20. Valid Parentheses
- leetcode20:Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- Leetcode: 20. Valid Parentheses
- [LeetCode]20. Valid Parentheses
- LeetCode(20)Valid Parentheses
- leetcode之top100_01_twoSsum/top100_20_ValidParentheses
- [LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
- LeetCode(20) Valid Parentheses
- LeetCode20:Valid Parentheses
- [leetcode 20] Valid Parentheses