java括号匹配
2014-01-24 00:00
176 查看
package com.duapp.itfanr; public class CharDemo{ public static boolean isMatch(String value) { int numCount = 0, numMatch = 0; for (int i=0; null != value && i<value.length(); i++) { char ch = value.charAt(i); if (ch == '{') { numCount ++; numMatch ++; } else if (ch == '}') { numCount --; } if (numCount < 0) { return false; } } return (numMatch > 0 && numCount == 0); } public static void main(String args[]) { System.out.println( "=========must false=====" ); System.out.println( isMatch(null) ); System.out.println( isMatch("") ); System.out.println( isMatch("ddddd") ); System.out.println( isMatch("d{dddd") ); System.out.println( isMatch("dd{}}ddd") ); System.out.println( isMatch("d}dd{dd") ); System.out.println( "=========must true=====" ); System.out.println( isMatch("dd{}ddd") ); System.out.println( isMatch("ddd{{}d}d") ); System.out.println( isMatch("d{d{d}d}d") ); } }
package com.duapp.itfanr; import java.util.LinkedList; public class CharDemo { private LinkedList<Character> stack = new LinkedList<Character>(); /** * @param args */ public static void main(String[] args) { CharDemo matcher = new CharDemo(); System.out.println(matcher.matches("{{{{}}a}ab}}")); } public boolean matches(String string) { if (string == null || string.length() == 0 || string.trim().equals("")) return true; int i = 0; while (i < string.length()) { char ch = string.charAt(i); if (ch == '{') { stack.push(ch); } else if (ch == '}') { if (stack.isEmpty()) return false; stack.pop(); } i++; } if (!stack.isEmpty()) return false; return true; } }参考: [1].
http://www.iteye.com/topic/1055854
相关文章推荐
- 对如下字符串(234453)[234]{2324}分析它的括号使用是否正确,括号匹配(Java实现)
- 括号匹配Java实现
- java版 利用栈实现括号匹配
- 匹配括号--Java
- java实现括号匹配
- java正则表达式匹配小括号内的内容
- java堆栈类解决括号匹配问题
- java括号匹配算法
- Java 正则表达式 匹配括号中的数字
- java版括号匹配问题
- 栈之括号匹配问题(java实现)
- Java程序练习-括号匹配
- 网易2016年Java工程师实习编程题目之括号匹配的验证
- java堆栈应用之括号匹配
- ACM括号匹配问题(java实现)
- 括号是否匹配 java实现
- Java程序练习-括号匹配
- 栈的应用之括号的匹配Java语言描述
- (java)括号匹配(简单)
- Java-利用栈实现括号匹配