[Leetcode] Valid Parentheses

来源:互联网 发布:天猫优质男装品牌知乎 编辑:程序博客网 时间:2024/06/06 05:57

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.

public class Solution {    public boolean isValid(String s) {        int len=s.length();        char[] sym=new char[len];         int count=0;        for(int i=0;i<len;i++)        {            switch (s.charAt(i))            {                case '(':                     sym[count]='(';                    count++;                    break;                case ')':                     count--;                    if(count<0) return false;                    if(sym[count]!='(') return false;                    break;                case '[':                     sym[count]='[';                    count++;                                       break;                case ']':                     count--;                    if(count<0) return false;                    if(sym[count]!='[') return false;                    break;                case '{':                     sym[count]='{';                    count++;                                       break;                case '}':                     count--;                    if(count<0) return false;                    if(sym[count]!='{') return false;                    break;                default:                    break;            }        }             if(count!=0) return false;             else return true;    }}



0 0
原创粉丝点击