import java.util.*; import java.util.Stack; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here //利用栈 filo的性质来处理 if(s.length() <= 1){ return false; } Stack<Character> stack = new Stack(); for(int i = 0;i < s.length();i++){ char next = s.charAt(i); //遇到左括号就入栈 if(next == '(' || next == '{' || next == '['){ stack.push(next); } else { //出栈对比 if(stack.isEmpty()){ return false; } else { char c = stack.pop(); if((next == ')' && c != '(') || (next == '}' && c != '{') || (next == ']' && c != '[')){ return false; } } } } if(stack.isEmpty()){ return true; } else { return false; } } }