java堆栈(Deque双向队列)应用之括号匹配

package tf; import java.util.ArrayDeque; import java.util.Deque; public class StackTest { public static void main(String[] args) { Deque stack = new ArrayDeque(); String str = "ab12+[b2(eef)w-45tyf[lk6{zxc29hg}]]"; boolean flag = true; char ch; for(int i = 0; i < str.length(); i++) { ch = str.charAt(i); if(ch == '(' || ch == '[' || ch == '{') { stack.offerFirst(ch); } else if(ch == ')') { if(stack.peekFirst() != null && stack.peekFirst() == '(') { stack.pollFirst(); } else { flag = false; break; } } else if(ch == ']') { if(stack.peekFirst() != null && stack.peekFirst() == '[') { stack.pollFirst(); } else { flag = false; break; } } else if(ch == '}') { if(stack.peekFirst() != null && stack.peekFirst() == '{') { stack.pollFirst(); } else { flag = false; break; } } } if(flag) { flag = stack.isEmpty(); } System.out.println("是否匹配:" + flag); } }

你可能感兴趣的:(Java面试大全)