【陪伴式刷题】Day 10|栈和队列| 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 '()[]{}'.

英文版地址

leetcode.com/problems/va...

中文版描述

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。

示例 1:

输入: s = "()"

输出: true

示例 2:

输入: s = "()[]{}"

输出: true

示例 3:

输入: s = "(]"

输出: false

提示:

  • 1 <= s.length <= 10(4)
  • s 仅由括号 '()[]{}' 组成

中文版地址

leetcode.cn/problems/va...

解题方法

俺这版

java 复制代码
class Solution {
    public boolean isValid(String s) {
        if (s == null) {
            return true;
        }
        char[] chars = s.toCharArray();
        int len = chars.length;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < len; i++) {
            if (stack.isEmpty()) {
                if (chars[i] == ')' || chars[i] == ']' || chars[i] == '}') {
                    return false;
                } else {
                    stack.push(chars[i]);
                }
            } else {
                if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{') {
                    stack.push(chars[i]);
                    continue;
                } else {
                    Character pop = stack.pop();
                    if (chars[i] == ')') {
                        if (pop.equals('(')) {
                            continue;
                        } else {
                            return false;
                        }
                    }
                    if (chars[i] == ']') {
                        if (pop.equals('[')) {
                            continue;
                        } else {
                            return false;
                        }
                    }
                    if (chars[i] == '}') {
                        if (pop.equals('{')) {
                            continue;
                        } else {
                            return false;
                        }
                    }
                }
            }
        }
        return  stack.isEmpty() ? true : false;
    }
}

复杂度分析

  • 时间复杂度:O(n)
  • 空间复杂度:O(n)
相关推荐
元亓亓亓39 分钟前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
仙俊红4 小时前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
_不会dp不改名_17 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌17 小时前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun17 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌17 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
爱编程的化学家19 小时前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
吃着火锅x唱着歌20 小时前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
愚润求学21 小时前
【贪心算法】day10
c++·算法·leetcode·贪心算法
Tisfy21 小时前
LeetCode 0966.元音拼写检查器:三个哈希表实现
leetcode·字符串·散列表·题解·哈希表