Valid Parentheses
来源:互联网 发布:冒险岛boss数据 编辑:程序博客网 时间:2024/05/16 20:29
Q:
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.
Solution:
public class Solution { 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 (c == '(' || c == '[' || c == '{') { stack.push(c); continue; } if (stack.isEmpty()) return false; if (c == ')' && stack.peek() == '(') stack.pop(); else if (c == ']' && stack.peek() == '[') stack.pop(); else if (c == '}' && stack.peek() == '{') stack.pop(); } if (stack.isEmpty()) return true; return false; }}
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- 开罗解放东路高架的福利国家
- 极限学习机 extreme learning
- 立刻就给了对方进攻的福利及股东
- 命令行创建指定屏幕大小的avd
- 关于gcc的一点小人性化提示
- Valid Parentheses
- [Leetcode] Convert Sorted List to Binary Search Tree Solution
- 三星 Samsung Galaxy Note/I9220/N7000 ZCLPP 4.0.4 国行ROM优化版
- Java程序员的JavaScript学习笔记(12——jQuery-扩展选择器)
- Binary Search Tree
- OS X10.10下HomeBrew的安装提示
- Length of Last Word
- 第一个周计划
- 修复PHP支持的标准JSON数据格式