LeetCode 20. 有效的括号

20. Valid Parentheses

Given a string s 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.
  3. Every close bracket has a corresponding open bracket of the same type.

Example 1:

Input: s = "()"

Output: true

Example 2:

Input: s = "()[]{}"

Output: true

Example 3:

Input: s = "(]"

Output: false

Constraints:

  • 1 <= s.length <= 10^4
  • s consists of parentheses only '()[]{}'.

解法思路:

1. 暴力:不断 replace 匹配的括号 -> ""   O(n^2)

   a. ()[]{}

   b. ((({[]}))) true

2. Stack. O(n) 

法一:

class Solution {
    public boolean isValid(String s) {
        // Time: O(n^2)
        int len = s.length();
        if (len % 2 != 0) {
            return false;
        }
        int n = len / 2;
        for (int i = 0; i < n; i++) {
            s = s.replace("()", "");
            s = s.replace("[]", "");
            s = s.replace("{}", "");
        }
        return s.length() == 0;
    }
}

 法二:

class Solution {
    public boolean isValid(String s) {
        // stack
        // O(n)
        int len = s.length();
        if (len % 2 != 0) {
            return false;
        }
        Map<Character, Character> pairs = new HashMap<Character, Character>() {
  {
                put(')', '(');
                put(']', '[');
                put('}', '{');
        }};
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.addFirst(ch);
            }
        }
        return stack.isEmpty();
    }
}

相关推荐

  1. LeetCode 20. 有效括号

    2024-01-02 11:46:05       37 阅读
  2. Leetcode 20. 有效括号

    2024-01-02 11:46:05       18 阅读
  3. LeetCode 20.有效括号

    2024-01-02 11:46:05       12 阅读
  4. Leetcode 20有效括号

    2024-01-02 11:46:05       13 阅读
  5. LeetCode20题 - 有效括号

    2024-01-02 11:46:05       40 阅读
  6. LeetCode_20_简单_有效括号

    2024-01-02 11:46:05       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 11:46:05       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 11:46:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 11:46:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 11:46:05       18 阅读

热门阅读

  1. Android 10.0 截屏流程

    2024-01-02 11:46:05       30 阅读
  2. Atlas Hook 导入 Hive 元数据

    2024-01-02 11:46:05       32 阅读
  3. KVM虚拟机部署K8S重启后/etc/hosts内容丢失

    2024-01-02 11:46:05       37 阅读
  4. 深入理解@Resource与@Autowired:用法与区别解析

    2024-01-02 11:46:05       35 阅读
  5. JDK下载地址

    2024-01-02 11:46:05       47 阅读
  6. GRU算法

    GRU算法

    2024-01-02 11:46:05      31 阅读
  7. ansible搭建和基本使用

    2024-01-02 11:46:05       40 阅读
  8. 数据库-期末考前复习-第1章-绪论

    2024-01-02 11:46:05       32 阅读