longlong6296

You\'re dealing with a string consisting of brackets. Write a program to examine whether the pairs of "{", "}", "(", ")", "[", "]" are correct or balanced. This means that each opening bracket must have a corresponding closing one (and vice versa) and they must go in the correct order.

For example, the program should print true for the string [()]{}{()()} and false for ()[]}.

The classic algorithm for solving this problem relies on using a stack.

create an instance of a stack;
traverse the input string;
if the current character is a starting bracket "(" or "{" or "[" then push it to the stack;
if the current is a closing bracket ")" or "}" or "]" then remove (pop) the top element from the stack. If the popped bracket does not match the starting bracket then parentheses are not balanced;
if there are some starting brackets left in the stack after completing traversal, then the parentheses are not balanced.
Report a typo
Sample Input 1:

([][])
Sample Output 1:

true
Sample Input 2:

({([])})
Sample Output 2:

true
Sample Input 3:

{{[()]]
Sample Output 3:

false


import java.util.*;

public class Main {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
   
        Stack<Character> stack = new Stack<>();
        
        boolean balanced = false;
        char a;
        char b;

        for (int i = 0; i < str.length(); i++) {
            a = str.charAt(i);
            if (a == \'(\' || a == \'[\' || a == \'{\') {
                stack.push(a);
            } else {
                if (a == \')\' || a == \']\' || a == \'}\') {
                    if (stack.size() == 0) {
                        balanced = false;
                        break;
                    }
                    b = stack.pop();
                    if ((b == \'{\' && a == \'}\') || (b == \'[\' && a == \']\') || (b == \'(\' && a == \')\')) {
                        balanced = true;
                    } else {
                        balanced = false;
                        break;
                    }
                }
            }
        }
        
        if (stack.size() > 0) {
            balanced = false;
        }
        
        System.out.println(balanced);
    }
}



import java.util.ArrayDeque;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        ArrayDeque<Character> brackets = new ArrayDeque<>();

        String openingBrackets = "({[";
        String closingBrackets = ")}]";
        char[] input = scanner.nextLine().toCharArray();

        boolean matched = true;

        for (char c : input) {
            if (openingBrackets.indexOf(c) > -1) {
                brackets.push(c);
                matched = false;
            } else if (closingBrackets.indexOf(c) > -1) {
                Character b = brackets.pollFirst();
                matched = b != null && openingBrackets.indexOf(b) == closingBrackets.indexOf(c);
            }
        }

        System.out.println(matched && brackets.size() == 0);
    }
}

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-02-24
  • 2021-05-24
  • 2021-08-25
  • 2022-03-08
  • 2022-01-03
  • 2021-09-20
  • 2021-05-14
猜你喜欢
  • 2021-07-13
  • 2022-12-23
  • 2021-08-14
  • 2018-12-13
  • 2021-10-03
  • 2021-12-09
相关资源
相似解决方案