java判断括号匹配问题

package bulecup;

import java.util.Stack;

public class Demo01 {

	public static boolean isValidate(String s){
		Stack<Character> a = new Stack<Character>();
		for(int i=0 ; i<s.length(); i++) {
			char c = s.charAt(i);
			if(c == '(')
				a.push(')');
			if(c == '[')
				a.push(']');
			if(c == '{')
				a.push('}');
			if(c == ')' || c == ']' || c == '}') {
				if(a.size() == 0) {
					return false;
				}
				if(a.pop() != c) {
					return false;
				}
			}
		}
		if(a.size() != 0) {
			return false;
		}
		return true;
	}
	
	public static void main(String[] args) {
		String s = "..(..(..[]..)..)";
		boolean a = isValidate(s);
		System.out.println(a);
	}
}

    原文作者:括号匹配问题
    原文地址: https://blog.csdn.net/qq_28663043/article/details/54017575
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞