20. 有效的括号

20. Valid Parentheses

题目描述

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

有效字符串需满足:

1. 左括号必须用相同类型的右括号闭合。
2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

LeetCode20. Valid Parentheses

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true

Java 实现

import java.util.Stack;

class Solution {
public boolean isValid(String s) {
if (s == null || s.length() == 0) {
return true;
}
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(') {
stack.push(')');
} else if (c == '[') {
stack.push(']');
} else if (c == '{') {
stack.push('}');
} else if (stack.isEmpty() || stack.pop() != c) {
return false;
}
}
return stack.isEmpty();
}
}

相似题目

参考资料

LeetCode 20. 有效的括号(Valid Parentheses)的更多相关文章

  1. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  2. leetcode第31题--Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  4. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  5. Java实现 LeetCode 20 有效的括号

    20. 有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...

  6. LeetCode 20:有效的括号 Valid Parentheses

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...

  7. LeetCode 20. 有效的括号(Valid Parentheses )

    题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字 ...

  8. LeetCode第[20]题(Java):Valid Parentheses

    题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

  9. [Swift]LeetCode20. 有效的括号 | Valid Parentheses

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

随机推荐

  1. C语言 memset函数及其用法

    定义 void *memset(void *s, int c, unsigned long n); 描述 将指针变量 s 所指向的前 n 字节的内存单元用一个“整数” c 替换,注意 c 是 int ...

  2. vue的ref试用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. 如何使用REDIS进行微服务间通讯

    如何使用REDIS进行微服务间通讯 尽可能避免service - to - service通信.为此,需要在服务之间推一个消息队列.回顾一下微服务的概念小型的,非常集中的进程彼此独立运行并且易于维护, ...

  4. 雪花ID实现新增数据同步

    雪花ID实现新增数据同步 GUID生成的ID,可以确保是唯一ID,但却是无序的,不适合用于数据同步. 雪花算法生成的ID(INT64)能够按照时间有序(升序)生成.只要电脑上的时间是正确的,新增的记录 ...

  5. 详解Unity Profiler内存分析问题

    在使用Unity开发游戏的过程中,借助Profiler来分析内存使用状况是至关重要的.但许多开发者可能还对Profiler中各项数据表示的含义不甚明确,今天我们Unity官方的技术工程师柳振东,将针对 ...

  6. JVM 扩展类加载器1

    1.创建类 public class MyTest19 { public static void main(String[] args) throws Exception { System.out.p ...

  7. 图片旋转 1. cv2.getRotationMatrix2D(获得仿射变化矩阵) 2. cv2.warpAffine(进行仿射变化)

    原文:https://www.cnblogs.com/my-love-is-python/p/10959612.html 1.rot_mat =  cv2.getRotationMatrix2D(ce ...

  8. JS正则表达式使用

    <script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+[_|\_ ...

  9. win10: ctrl+shift不能切换输入法的问题

    习惯了使用Ctrl+Shift切换输入法,乍然切换到Win10,很不适应,可用以下方法修改之. 1. 点击开始菜单,点击“设置”按钮   2. 选择“时间与语言” 3. 选择“区域和语言”按钮,查看右 ...

  10. 约翰·麦斯威尔 | John C. Maxwell | A leader is one who knows the way, goes the way, and shows the way.

    约翰·麦斯威尔_百度百科https://baike.baidu.com/item/%E7%BA%A6%E7%BF%B0%C2%B7%E9%BA%A6%E6%96%AF%E5%A8%81%E5%B0%9 ...