【陪伴式刷题】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)
相关推荐
大二转专业2 小时前
408算法题leetcode--第24天
考研·算法·leetcode
__AtYou__9 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
转调9 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
huanxiangcoco10 小时前
152. 乘积最大子数组
python·leetcode
希望有朝一日能如愿以偿12 小时前
力扣题解(飞机座位分配概率)
算法·leetcode·职场和发展
Espresso Macchiato12 小时前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
数据分析螺丝钉13 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
￴ㅤ￴￴ㅤ9527超级帅13 小时前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
鱼跃鹰飞14 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试
源代码•宸17 小时前
Leetcode—76. 最小覆盖子串【困难】
c++·经验分享·算法·leetcode·滑动窗口