论坛首页 Java企业应用论坛

华为面试题!

浏览 28018 次
精华帖 (0) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (5)
作者 正文
   发表时间:2011-11-07  
opal 写道
public class T {
	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") );
	}
}



这个解法非常不错啊,只是漏了一种情况  dasda}{dsd{}
0 请登录后投票
   发表时间:2011-11-07  
这个简单,遍历字符串每个字符,遇到{ 添加到容器中, 遇到} 在取出容器中的{
如果到最后,容器为空,就是匹配了。
0 请登录后投票
   发表时间:2011-11-08  
jyyuanqi 写道
opal 写道
public class T {
	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") );
	}
}



这个解法非常不错啊,只是漏了一种情况  dasda}{dsd{}


你确定????
0 请登录后投票
   发表时间:2011-11-09  
opal 写道
public class T {
	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") );
	}
}





你这是面向过程的思维,我希望有大牛用面向对象的思维方式去解决!
0 请登录后投票
   发表时间:2011-11-09  
只需要返回这两个符号数量是否相等就行了
0 请登录后投票
   发表时间:2011-11-17  
stormtoy 写道
只需要返回这两个符号数量是否相等就行了

这个方法确实很有效。LinkedList 就没有必要了。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics