LeetCode:括号匹配问题的解决

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:


Input: "()"
Output: true

Example 2:


Input: "()[]{}"
Output: true

import java.util.Stack;

/**
 * @author zhangyu
 * @version V1.0
 * @ClassName: TestBrackets
 * @Description: TOTO
 * @date 2018/10/12 13:34
 **/

public class TestBrackets {
    public static void main(String[] args) {
        String s = "(])";
        boolean flag = isValid(s);
        System.out.println(flag);
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //如果字符串为空就直接返回
        if (s.equals("")) {
            return true;
        }
        //如果不为空就将字符串变成字符数组
        char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            if (chs[i] == '{' || chs[i] == '[' || chs[i] == '(') {
                stack.push(chs[i]);
            }

            /*//当栈为空时候,判断字符的值
            if (stack.isEmpty()) {
                if (chs[i] == ']' || chs[i] == '}' || chs[i] == ')') {
                    return false;
                }
            } else { //当栈不为空时候,判断栈的情况
                if (chs[i] == '}') {
                    if (stack.peek() == '{') {
                        stack.pop();
                    } else {
                        return false;
                    }
                }
                if (chs[i] == ']') {
                    if (stack.peek() == '[') {
                        stack.pop();
                    } else {
                        return false;
                    }
                }
                if (chs[i] == ')') {
                    if (stack.peek() == '(') {
                        stack.pop();
                    } else {
                        return false;
                    }
                }
            }*/
            if (stack.isEmpty()) {
                if (chs[i] == ']' || chs[i] == '}' || chs[i] == ')') {
                    return false;
                }
            }

            if (chs[i] == ']') {
                if (!stack.isEmpty() && stack.peek() == '[') {
                    stack.pop();
                } else {
                    return false;
                }
            }

            if (chs[i] == ')') {
                if (!stack.isEmpty() && stack.peek() == '(') {
                    stack.pop();
                } else {
                    return false;
                }
            }

            if (chs[i] == '}') {
                if (!stack.isEmpty() && stack.peek() == '{') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }

        //如果栈为空表示匹配,如果栈不为空表示不匹配
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zy345293721/article/details/83029129