答复: 华为面试题!
程序员文章站
2022-07-14 16:56:50
...
忙里偷闲 回复一篇帖子
原帖子地址:http://www.iteye.com/topic/1055854
原帖子地址: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; } }