pahdmbczpe 发表于 2016-6-6 08:50:45

答复: 华为面试题!

忙里偷闲 回复一篇帖子
原帖子地址:http://www.iyunv.com/topic/1055854

package jdk;
import java.util.Stack;
public class StackTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(isMatch("{}"));
System.out.println(isMatch("{"));
System.out.println(isMatch("{kljdfj}}fj;fj"));
System.out.println(isMatch("{}}"));
System.out.println(isMatch("{{fdsfs}}{}"));
}
public static boolean isMatch(final String str) {
Stack<Character> stack = new Stack<Character>();
for (int i=0;i<str.length();i++) {
if (str.charAt(i) == '{') {
stack.push(str.charAt(i));
continue;
}
if (str.charAt(i) == '}'&& stack.size() != 0) {
stack.pop();
//System.out.println("{ }");
continue;
}
if (str.charAt(i) == '}'&& stack.size() == 0)
return false;
}
if (stack.size() > 0)
return false;
return true;
}
}
页: [1]
查看完整版本: 答复: 华为面试题!