Topic describes

Given a string containing only ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘, ‘]’, check whether the string is valid.

A valid string must meet the following requirements:

An open parenthesis must be closed with a close parenthesis of the same type. The left parentheses must be closed in the correct order.Copy the code

Note that an empty string can be considered a valid string.

Example 1:

Input: “()” Output: true

Example 2:

Input: “()[]{}” Output: true

Example 3:

Input: “(]” Output: false

Example 4:

Input: “([)]” output: false

Example 5:

Input: “{[]}” Output: true

Answer key 1

import java.util.Scanner;
import java.util.Stack;

public class leetcode20 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        boolean valid = isValid(str);
        System.out.println(valid);

    }
    public static boolean isValid(String s) {
        int length = s.length();
        if (length%2= =1) {return  false;
        }
        Stack<String> left = new Stack<>();
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            String single = String.valueOf(aChar);
            if ("(".equals(single)||"{".equals(single)||"[".equals(single)){
                left.push(single);
            }else{

                if (left.size()==0) {return  false;
                }
                String rightsingle = left.pop();
                if (")".equals(single)){
                  if (!"(".equals(rightsingle)){
                      return false; }}else   if ("}".equals(single)){
                    if (!"{".equals(rightsingle)){
                        return false; }}else   if ("]".equals(single)){
                    if (!"[".equals(rightsingle)){
                        return false; }; }}}if(left.size()! =0) {return  false;
        }
        return  true; }}Copy the code

Answer key 2

import java.util.*;

public class leetcode20version3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        boolean valid = isValid(str);
        System.out.println(valid);
    }
    public static boolean isValid(String s){
        int n = s.length();
        if (n % 2= =1) {
            return false;
        }

        Map<Character, Character> pairs = new HashMap<Character, Character>() {{
            put(') '.'(');
            put('] '.'[');
            put('} '.'{');
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() ! = pairs.get(ch)) {return false;
                }
                stack.pop();
            } else {
                stack.push(ch); }}return stack.isEmpty(); }}Copy the code

Above is leetcode 20. Answer the following questions, effective brackets if there are any help please thumb up attention also support, if in doubt, direct messages can be seen after can help solve this blog is mainly focus on the data structure in algorithm and Java development, operating systems, computer networks, think of my article helpful friend can focus on me, doubt can comment on direct messages, Meet is fate, we see high