leetcode 52: valid parentheses
来源:互联网 发布:聋哑女孩淘宝模特 编辑:程序博客网 时间:2024/04/29 04:13
Valid ParenthesesJan 30 '12
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.
class Solution: # @return a boolean def isleft(self, c): return c=='(' or c=='{' or c=='[' def ispair(self, l, r): if l=='(': return r==')' elif l=='[': return r==']' elif l=='{': return r=='}' else: return False def isValid(self, s): stack = [] for c in s: if self.isleft(c) : stack.append(c) elif not stack: return False elif self.ispair(stack[-1], c): stack.pop() else: return False return not stack
class Solution {public: bool isValid(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function stack<char> myStack; char c; for( int i=0; i<s.size(); i++) { if( s[i] == '(' || s[i] == '[' || s[i] == '{') { myStack.push( s[i] ); } else if( myStack.empty() ) { return false; } else if( s[i] == ')') { c = myStack.top(); if( c!= '(' ) return false; else myStack.pop(); } else if( s[i] == ']') { c = myStack.top(); if( c!= '[' ) return false; else myStack.pop(); } else if( s[i] == '}') { c = myStack.top(); if( c!= '{' ) return false; else myStack.pop(); } else { return false; } } return myStack.empty(); }};
public class Solution {//null, "", "(){}[]" "({[]})" "(({}]" public boolean isValid(String s) { // Start typing your Java solution below // DO NOT write main() function if(s==null || s.length()==0) return true; Stack<Character> stack = new Stack<Character>();for(int i=0; i<s.length(); i++) {char c = s.charAt(i);if(c=='(' || c=='{' || c=='[' ) { stack.push(c); } else if(stack.isEmpty() ){ return false; } else { boolean r = isValid(stack.peek(), c); if(r) { stack.pop(); } else { return false; } }}return stack.isEmpty(); } private boolean isValid(char l, char r) { if(l=='(') return r==')'; if(l=='[') return r==']'; if(l=='{') return r=='}'; return false; }}
- leetcode 52: valid parentheses
- LeetCode: Valid Parentheses
- LeetCode: Longest Valid Parentheses
- LeetCode: Valid Parentheses
- LeetCode Longest Valid Parentheses
- [Leetcode] Valid Parentheses
- LeetCode: Longest Valid Parentheses
- [Leetcode] Longest Valid Parentheses
- LeetCode : Valid Parentheses
- [leetcode]longest Valid Parentheses(!!)
- Leetcode: Valid Parentheses
- [Leetcode] Longest Valid Parentheses
- leetcode Longest Valid Parentheses
- 【leetcode】Valid Parentheses
- 【leetcode】Longest Valid Parentheses
- [LeetCode]Longest Valid Parentheses
- [LeetCode]Valid Parentheses
- LeetCode-Valid Parentheses
- Building Collada
- ARRAY专题一:数组(ARRAY)与ARRAYLIST的主要区别:效率、类型识别和PRIMITIVE TYPE。
- linux c实现超时、非阻塞socket的函数select
- DB2巡检步骤 【监控】
- HR有用的FM读评估路径 和ROOT
- leetcode 52: valid parentheses
- 表迁移后注意事项 【迁移与备份】
- 随想0125
- A4 : prime number within N
- 48(70)---windows internals mark david alex
- About Recursion
- CentOS 6.3定制自动安装
- 说说字符集和编码
- IOS学习笔记41——苹果推送机制APNs(二)