class Solution { public boolean isValid(String s) { if(s==null||s.length()==0){ return true; } //将字符串转换为char[] char[] c=s.toCharArray(); Stack<Character> stack=new Stack<Character>(); for(int i=0;i<c.length;i++){ if(c[i]=='('||c[i]=='{'||c[i]=='['){ stack.push(c[i]); } if(c[i]==')'){ //这个地方判断条件一定要注意顺序 if(stack.isEmpty()||stack.pop()!='('){ return false; } } if(c[i]=='}'){ if(stack.isEmpty()||stack.pop()!='{'){ return false; } } if(c[i]==']'){ if(stack.isEmpty()||stack.pop()!='['){ return false; } } } return stack.isEmpty(); } }