数据结构一.用栈检测括号匹配

代码如下

package dataStruct;
/**
 * 栈检测括号匹配
 */
import java.util.Scanner;
import java.util.Stack;

public class CheckBracket {
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);  
     
     int n= sc.nextInt();
       
     Stack<Character> stack = null;  
       
     while(n!=0){  
           
         //从控制台读入一个测试字符串[]() [(])  
         String str = sc.next();  
         //如果该输入字符串为奇数,说明不匹配  
         if(str.length() % 2 == 1){  
             System.out.println("No");  
         }else{  
             //说明字符是偶数  
             stack = new Stack<Character>();  
             for(int i=0;i<str.length();i++){  
                 if(stack.isEmpty()){  
                     //如果栈是空的  
                     stack.push(str.charAt(i));  
                 }else if(stack.peek() == '[' && str.charAt(i) == ']' || stack.peek() == '(' && str.charAt(i) == ')'){  
                     //说明此时栈中字符不是空的,并且符合一对一对的,就出栈  
                     stack.pop();  
                 }else{                      
                     stack.push(str.charAt(i));  
                 }  
             }  
               
             if(stack.isEmpty()){  
                 //如果栈是空的,说明括号匹配  
                 System.out.println("Yes");  
             }else{  
                 //说明栈不为空,括号不匹配  
                 System.out.println("No");  
             }  
         }  
           
         n--;  
     }  
}
}

猜你喜欢

转载自blog.csdn.net/gegeyanxin/article/details/81987058