https://leetcode-cn.com/problems/valid-parentheses/

    1. public boolean isValid4(String s) {
    2. if (s.length()==0 || s.length()==1) {
    3. return false;
    4. }
    5. HashMap<Character, Character> newmap = new HashMap<>();
    6. newmap.put('(', ')');
    7. newmap.put('[', ']');
    8. newmap.put('{', '}');
    9. Stack<Character> stack = new Stack<>();
    10. for (int i = 0; i < s.length(); i++) {
    11. char c = s.charAt(i);
    12. if (c == '(' || c == '[' || c == '{' ) {
    13. stack.push(c);
    14. }else {
    15. if (stack.isEmpty()) return false;
    16. char left = stack.pop();
    17. if (c != newmap.get(left)) {
    18. return false;
    19. }
    20. }
    21. }
    22. return stack.isEmpty();
    23. }

    20年b站的面试题,第二遍做