java括号匹配
2014-12-20 00:00
295 查看
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
相关文章推荐
- Java程序练习-括号匹配
- 括号匹配Java版
- java堆栈应用之括号匹配
- 栈的应用之括号的匹配Java语言描述
- java利用栈实现括号()[]匹配问题
- java括号匹配算法
- java堆栈类解决括号匹配问题
- 用栈实现括号匹配检验(java)
- java的括号匹配程序
- java 中的括号匹配问题
- java--一道简单的括号匹配问题
- 判断表达式括号是否匹配java和js版本
- [原]NYOJ-括号匹配-2(java)
- 括号是否匹配 java实现
- java括号匹配
- Java程序练习-括号匹配
- [原]NYOJ-括号匹配-2(java)
- Java 括号匹配问题
- 华为-成研所笔试-java-括号匹配
- Java 用栈解决括号匹配问题