---
title: "java实现括号匹配"
author: "hy2012_campus"
date: "2014-12-10T13:45:24Z"
tags: []
categories: ["开源软件"]
slug: "java"
draft: false
lastmod: "2014-12-10T13:45:24Z"
id: 2164801
url: "https://hy2012.iteye.com/blog/2164801"
---

```
public boolean bracketMatch(String str){
		Stack<Integer> s = new Stack<Integer>();
		for(int i = 0; i < str.length(); i++){
			char c = str.charAt(i);
			switch(c){
				case '{':
				case '[':
				case '(': s.push(Integer.valueOf(c));break;
				case '}':
					if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='{'){
						break;
					}else{
						return false;
					}
				case ']':
					if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='['){
						break;
					}else{
						return false;
					}
				case ')':
					if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='('){
						break;
					}else{
						return false;
					}
			}
		}
		if(s.isEmpty()){
			return true;
		}else{
			return false;
		}
	}
```