论坛首页 Java企业应用论坛

华为面试题!

浏览 28019 次
精华帖 (0) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (5)
作者 正文
   发表时间:2011-05-24   最后修改:2011-05-24
package com;

import java.util.ArrayList;
import java.util.List;

public class Test06 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String testStr = "{12Sf{你}{}}好,3.abc";
		if (existStr(testStr))
			System.out.println("true");
		else
			System.out.println("false");
	}

	private static boolean existStr(String str) {

		if (str.indexOf("{") >= 0 && str.indexOf("}") >= 0) {
			List flist = new ArrayList();
			for (int k = 0; k < str.length(); k++) {
				if (str.charAt(k) == '{') {
					flist.add(str.charAt(k));
				} else if (str.charAt(k) == '}') {
					try {
						flist.remove(0);
					} catch (IndexOutOfBoundsException e) {
						return false;
					}
				}

			}
			if (!flist.isEmpty())
				return false;

			return true;
		} else
			return false;
	}

}

这样可以吗?
0 请登录后投票
   发表时间:2011-05-24  
import java.util.Stack;

public class MatchReg {

	private static Stack<Character> s = new Stack<Character>();

	private static void match(String reg) {
		char cArray[] = reg.toCharArray();
		for (char c : cArray) {
			if (c == '{')
				s.add(c);
			else if (c == '}') {
				s.pop();
			}
		}

	}

	public static void main(String[] args) {
		String str = "a{b{c{f{g{}}}d{}e{h{}}}}i{}";
		match(str);
	}
}
0 请登录后投票
   发表时间:2011-05-24  
 public static int demo(String str){
	 byte[] bytes=str.getBytes();
	 Stack stack=new Stack();
	 for(byte ch : bytes){
		 if(ch=='{')
		 stack.push(ch);
		 else if(ch=='}')
			 stack.pop();
	 }
	 return stack.size();
 }
0 请登录后投票
   发表时间:2011-05-24  
下面的程序应该满足条件吧
import java.util.Stack;


public class AnotherClass {

public static boolean check(String str){
Stack<Character> stk = new Stack<Character>();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(c=='{'){
stk.push(c);
}else if(c=='}'){
if(stk.isEmpty()){
return false;
}
stk.pop();
}
}
if(stk.isEmpty()){
return true;
}
return false;
}


public static void main(String[] args){
String str = "asfafasfgasf{asfasfasfaf}sfdasf{}afasfd}afasF";
boolean result = check(str);
System.out.println("result="+result);
}
}
0 请登录后投票
   发表时间:2011-05-24   最后修改:2011-05-24
下面的程序应该可以的
import java.util.Stack;


public class AnotherClass {
	
	public static boolean check(String str){
		Stack<Character> stk = new Stack<Character>();
		for(int i=0;i<str.length();i++){
			char c = str.charAt(i);
			if(c=='{'){
				stk.push(c);
			}else if(c=='}'){
				if(stk.isEmpty()){
					return false;
				}
				stk.pop();
			}
		}
		if(stk.isEmpty()){
			return true;
		}
		return false;
	}
	
	
	public static void main(String[] args){
		String str = "asfafasfgasf{asfasfasfaf}sfdasf{}afasfd}afasF";
		boolean result = check(str);
		System.out.println("result="+result);
	}
}
0 请登录后投票
   发表时间:2011-05-24  
int count = 0;
for (int i = 0; i < str.length(); i++) {
  switch (str.charAt(i)) {
    case '{':
      count++;
      break;
    case '}':
      count--;
      break;
    default:
  }
  if (count < 0) {
    throw new IllegalArgumentException("'}' before '{' in string: " + str);
  }
}

if (count != 0) {
  throw new IllegalArgumentException("Braces not match in string: " + str);
}

 

0 请登录后投票
   发表时间:2011-05-24  
NanguoCoffee 写道
明显是考数据结构:Stack

- -
0 请登录后投票
   发表时间:2011-05-24  
看完楼上的代码之后,我倾向于后者。
①后者的代码从解题思路上看,目的性更加明确
②代码解析度上看,较前者也有优势。
0 请登录后投票
   发表时间:2011-05-24   最后修改:2011-05-24
package com.java.wlq.util;

import java.util.Stack;

public class StrackTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "{}{}{}{}";
		System.out.println(match(str));
	}
	
	/**
	 * 判断字符串中‘{’与‘}’是否匹配
	 * 
	 * @param str
	 * @return true:匹配;false:不匹配
	 */
	public static boolean match(String str){
		if (null == str || str.length() == 0) return false;
		Stack<Character> stk = new Stack<Character>();
		for (int i=0; i< str.length(); i++) {
			char ch = str.charAt(i);
			if (ch == '{') {
				stk.push(ch);
			} else if (ch == '}') {
				if (stk.isEmpty()) return false;
				stk.pop();
			}
		}
		if (stk.isEmpty()) {
			return true;
		}
		return false;
	}
}
0 请登录后投票
   发表时间:2011-05-25   最后修改:2011-05-25
表示不需要用Stack,int就行了,如果题目仅考查结果的话。
0 请登录后投票
论坛首页 Java企业应用版

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