LeetCode 20. Valid Parentheses

来源:互联网 发布:python 数据清洗 框架 编辑:程序博客网 时间:2024/06/07 06:39
public class Solution {    public boolean isValid(String s) {        if(s.length()%2==1)          return false;          Stack stack= new Stack();        char [] c =s.toCharArray();        for(int i=0;i<s.length();i++)        {        if(c[i]=='(' || c[i]=='{' || c[i]=='['){        stack.push(c[i]);        }else if(c[i]==')'){        if(!stack.isEmpty()){        char temp=(char) stack.pop();        if(temp!='(')        return false;        }else         return false;                }else if(c[i]=='}'){        if(!stack.isEmpty()){        char temp=(char) stack.pop();        if(temp!='{')        return false;        }else         return false;                }else if(c[i]==']'){        if(!stack.isEmpty()){        char temp=(char) stack.pop();        if(temp!='[')        return false;        }else         return false;                }        }        if(stack.isEmpty())return true;        else        return false;    }}

0 0
原创粉丝点击