📜  FIFO 括号黑客等级 - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:16.333000             🧑  作者: Mango

代码示例1
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
     String input=sc.next();
     Stack stacky = new Stack<>();
     for (int i = 0; i < input.length(); i++) {
        if (!stacky.isEmpty()) {
            switch(input.charAt(i)) {
                case '}' : if (stacky.peek() == '{') {
                    stacky.pop();
                } break;
                case ']' : if (stacky.peek() == '[') {
                    stacky.pop();
                } break;
                case ')' : if (stacky.peek() == '(') {
                    stacky.pop();
                } break;
                default: stacky.push(input.charAt(i));
          }
        } else {
            stacky.push(input.charAt(i));
        } 
     }
     System.out.println(stacky.isEmpty());
  }