日期:2014-05-20  浏览次数:20598 次

华为的一道机试,你会吗,用java写出来
华为 机试

------解决方案--------------------
   public int find(String inputstr){
        String s;
        int a = 1;
        if(inputstr.contains("[")){
            if(inputstr.contains("]")){
                if(inputstr.indexOf("[") < inputstr.indexOf("}")){
                s = inputstr.substring(inputstr.indexOf("["),inputstr.indexOf("}")+1);
                if(s.contains("(")){
                    if(s.contains(")")){
                        if(s.indexOf("(") < s.indexOf(")")){
                            a = 0;
                        }
                        else{
                            a = 1;
                        }
                    }else{
                        a = 1;
                    }
                }else{
                    a = 1;
                }
                }else{
                    a = 1;
                }
            }else{
                a = 1;
            }
        }else{
            if(!inputstr.contains("]") && !inputstr.contains("(") && !inputstr.contains(")"))
            {
                a = 0;
            }
        }
        return a;

    }
数组的比较烦,思路差不多
------解决方案--------------------
public static int checkStr(String s){

int resultValue = 0; //初始返回值
LinkedList<Byte> ll= new LinkedList<Byte>(); //用于存放括号的栈
byte[] bs = s.getBytes();
//遍历String
for(byte b : bs){
//如果是左括号,存入栈
if(b == '('
------解决方案--------------------
b == '['){
ll.add(b);
}else if(b == ')'){
//如果是‘)’,取栈中最后的括号,判断是否匹配
//若不匹配,则返回1;
if(ll.getLast() != '('){
resultValue = 1;
break;
}
}else if(b == ']'){
if(ll.getLast() == '['){
resultValue = 1;
break;
}
}
}
return resultValue;
}
------解决方案--------------------

public static int isMatch(String str){
int  result = 0;
LinkedList<Character> list = new LinkedList<Character>();