欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

答复: 华为面试题!

程序员文章站 2022-07-14 16:57:26
...
忙里偷闲 回复一篇帖子
原帖子地址:http://www.iteye.com/topic/1055854

package jdk;

import java.util.Stack;

public class StackTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(isMatch("{}"));
		System.out.println(isMatch("{"));
		System.out.println(isMatch("{kljdfj}}fj;fj"));
		System.out.println(isMatch("{}}"));
		System.out.println(isMatch("{{fdsfs}}{}"));
		
	}
	
	public static boolean isMatch(final String str) {
		Stack<Character> stack = new Stack<Character>();
		for (int i=0;i<str.length();i++) {
			if (str.charAt(i) == '{') {
				stack.push(str.charAt(i));
				continue;
			} 
			if (str.charAt(i) == '}'  && stack.size() != 0) {
				stack.pop();
//				System.out.println("{ }");
				continue;
			}
			if (str.charAt(i) == '}'  && stack.size() == 0)
				return false;
		}
		if (stack.size() > 0) 
			return false;
		return true;
	}
}