0

I have some specific task. We have String like "(()[]<>)" or something familiar with this. A question in my interview qustion was how check either String is correct or incorrect. For example: "()[]<>" - true, "([)" - false, "[(])" - false, "([<>])" - true. Thank you guys very much! I can' t take what's wrong with my code. Thank a lot guys!!! Please help!

import java.util.Stack;

public class Test {

public static void main(String[] args) {

    String line = "(<>()[])";
    Test test = new Test();
    boolean res = test.stringChecker(line);
    System.out.println(res);
}

public boolean stringChecker(String line){
    boolean result = false;
    char letter = '\u0000';
    char[] arr = line.toCharArray();
    Stack<Character> stack = new Stack();

    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == '(' || arr[i] == '[' || arr[i] == '<') {
            stack.push(arr[i]);
        }
        if(arr[i] == ')' || arr[i] == ']' || arr[i] == '>'){
                if(stack.peek() == arr[i]){
                    result = true;
                    stack.pop();

            }
        }
    }

    return result;
}

}

4

2 回答 2

2

(0) 您正在推动 < ( 和 { 但在您的窥视中您正在检查 >、) 和 }

(1) 您从结果 false 开始,并在第一次成功匹配时将其设置为 true。相反,您应该从结果 true 开始,并在第一个失败的匹配时将其设置为 false。

(2) 当字符用完时,应检查堆栈是否为空。

(3) 在窥视之前,您应该检查堆栈是否为空。

(4) 您可能想要检查不期望的字符。

于 2013-11-02T18:28:13.597 回答
0

除了@TheodoreNorvell 在这里的解释是实现的样子

public boolean stringChecker(String input) {
    boolean result = true;
    char[] arr = input.toCharArray();
    Stack<Character> stack = new Stack<>();
    try {
        for (int i = 0; result && i < arr.length; i++) {
            if (arr[i] == '(' || arr[i] == '[' || arr[i] == '<') {
                stack.push(arr[i]);
            } else if(arr[i] == ')') {
                Character c = stack.pop();
                result = c.equals('(');
            } else if(arr[i] == ']') {
                Character c = stack.pop();
                result = c.equals('[');
            } else if(arr[i] == '>') {
                Character c = stack.pop();
                result = c.equals('<');
            } else {
                // found some char that is not allowed
                // here it is not just ignored,
                // it invalidates the input  
                result = false;
            }
        }
        // when the teher is not more chars in the array
        // the stack has to be empty 
        result = result  && stack.isEmpty() ;
    } catch(EmptyStackException e) {
        // found a closing bracket in the array 
        // but there is nothing on the stack
        result = false;
    }
    return result;
}

@Test
public void stringChecker() {
    Assert.assertTrue(stringChecker("[]"));
    Assert.assertTrue(stringChecker("[(<>)]"));
    Assert.assertFalse(stringChecker("([<>)]"));
    Assert.assertFalse(stringChecker(">"));
    // invalid char
    Assert.assertFalse(stringChecker("<[]e>"));
    // stack is not empty
    Assert.assertFalse(stringChecker("("));
}

请注意,在这种情况下,switch-case语句比if-else if-else.

于 2013-11-02T18:51:06.017 回答