Valid Parentheses
来源:互联网 发布:淘宝服务市场推广 编辑:程序博客网 时间:2024/05/22 13:22
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.
用stack做。恩先复习下stack的用法。。。
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); else if(c==')'||c=='}'||c==']'){ if(stack.size()==0) return false; char cpop=stack.pop(); if(c=='}'&&cpop=='{') continue; if(c==')'&&cpop=='(') continue; if(c==']'&&cpop=='[') continue; else return false; } } return stack.size()==0; }}
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
- 没智能手机会死星人的福音来了
- apache和nginx原理上的不同之处
- PHP + Memcache 实现多服务器session共享
- RaspberryPie安装操作系统及前期的准备工作
- C语言 16进制字符串 转 16进制 方法
- Valid Parentheses
- Android编码规范
- C语言 内存操作函数
- UVALive 6669 Hidden Tree DP
- STL学习(六)heap学习
- 移植64位发生的问题(跟cltq指令有关)
- DSP/BIOS硬件中断管理
- jQuery 2.0发布,不支持IE 6/7/8
- test