LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号
20. Valid Parentheses
题目描述
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
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();
}
}
相似题目
参考资料
- https://leetcode.com/problems/valid-parentheses/
- https://leetcode-cn.com/problems/valid-parentheses/
LeetCode 20. 有效的括号(Valid Parentheses)的更多相关文章
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- Java实现 LeetCode 20 有效的括号
20. 有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...
- LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...
- LeetCode 20. 有效的括号(Valid Parentheses )
题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字 ...
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- [Swift]LeetCode20. 有效的括号 | Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- fork()函数 图解
code #include<stdio.h> #include <getopt.h> #include<iostream> #include<string&g ...
- EasyTrader踩坑之旅(三)
快速阅读 用THSTrader 调试同花顺自动下单的过程 . 主要原理是利用python函数pywinauto 自动获取同花顺上相应控件的值,进行模拟自动化的操作,不得不说python函数库的强大 ...
- UDP如何实现可靠传输
概述 UDP不属于连接协议,具有资源消耗少,处理速度快的优点,所以通常音频,视频和普通数据在传送时,使用UDP较多,因为即使丢失少量的包,也不会对接受结果产生较大的影响. 传输层无法保证数据的可靠传输 ...
- 安卓开发实战之app之版本更新升级(DownloadManager和http下载)完整实现
转载: https://blog.csdn.net/u013278099/article/details/52692008 前言 本文将讲解app的升级与更新.一般而言用户使用App的时候升级提醒有两 ...
- cross socket tcp客户端开发
cross socket tcp客户端开发 uses Net.SocketAPI, Net.CrossSocket.Base, Net.CrossSocket FCrossTcp: ICrossSoc ...
- 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 ...
- 判断x的m次方和y的m次方末尾三位数是否相等
/*==============================================对于任意给定的两个正整数x和y,是否存在一个不超过100的正整数m使得x^m与y^m的末尾三位数相等呢? ...
- mosquitto设置用户名和密码
https://blog.csdn.net/qq_22111417/article/details/84142509 7.设置用户名和密码 找到用户密码文件在安装bin下: 1: 打开mosquitt ...
- flutter Form表单
import 'package:flutter/material.dart'; class FormDemo extends StatelessWidget { @override Widget bu ...
- Mac 快速进入mysql命令行
1.终端输入进入bin 目录 cd /usr/local/mysql/bin/ 2.mysql登录,输入密码即可 ./mysql -uroot -p 前提:mysql 服务已启动