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. fork()函数 图解

    code #include<stdio.h> #include <getopt.h> #include<iostream> #include<string&g ...

  2. EasyTrader踩坑之旅(三)

    快速阅读 ​ 用THSTrader 调试同花顺自动下单的过程 . 主要原理是利用python函数pywinauto 自动获取同花顺上相应控件的值,进行模拟自动化的操作,不得不说python函数库的强大 ...

  3. UDP如何实现可靠传输

    概述 UDP不属于连接协议,具有资源消耗少,处理速度快的优点,所以通常音频,视频和普通数据在传送时,使用UDP较多,因为即使丢失少量的包,也不会对接受结果产生较大的影响. 传输层无法保证数据的可靠传输 ...

  4. 安卓开发实战之app之版本更新升级(DownloadManager和http下载)完整实现

    转载: https://blog.csdn.net/u013278099/article/details/52692008 前言 本文将讲解app的升级与更新.一般而言用户使用App的时候升级提醒有两 ...

  5. cross socket tcp客户端开发

    cross socket tcp客户端开发 uses Net.SocketAPI, Net.CrossSocket.Base, Net.CrossSocket FCrossTcp: ICrossSoc ...

  6. ORA-39142: incompatible version number 5.1 in dump file

    ORA-39142: incompatible version number 5.1 in dump file http://blog.itpub.net/26664718/viewspace-214 ...

  7. 判断x的m次方和y的m次方末尾三位数是否相等

    /*==============================================对于任意给定的两个正整数x和y,是否存在一个不超过100的正整数m使得x^m与y^m的末尾三位数相等呢? ...

  8. mosquitto设置用户名和密码

    https://blog.csdn.net/qq_22111417/article/details/84142509 7.设置用户名和密码 找到用户密码文件在安装bin下: 1: 打开mosquitt ...

  9. flutter Form表单

    import 'package:flutter/material.dart'; class FormDemo extends StatelessWidget { @override Widget bu ...

  10. Mac 快速进入mysql命令行

    1.终端输入进入bin 目录 cd /usr/local/mysql/bin/ 2.mysql登录,输入密码即可 ./mysql -uroot -p 前提:mysql 服务已启动