leetcode129valid-parentheses
题目描述
括号必须以正确的顺序关闭,"()"和"()[]{}"都是合法的括号序列,但"(]"和"([)]"不合法。
The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not.
输出
true
class Solution {
public:
/**
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
int len=s.size();
stack <char> sta;
for (int i=0;i<len;i++){
if (s[i]=='(')sta.push(')');
else if (s[i]=='[') sta.push(']');
else if (s[i]=='{')sta.push('}');
else if (sta.empty())return false;
else if (sta.top()!=s[i])return false;
else sta.pop();
}
return sta.empty();
}
};
leetcode129valid-parentheses的更多相关文章
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- No.022:Generate Parentheses
问题: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Trie树【字典树】浅谈
最近随洛谷日报看了一下Trie树,来写一篇学习笔记. Trie树:支持字符串前缀查询等(目前我就学了这些qwq) 一般题型就是给定一个模式串,几个文本串,询问能够匹配前缀的文本串数量. 首先,来定义下 ...
- CN1,CN2 GT和CN2 GIA的区别
用一句话来概括,CN1主要定位于承载普通质量的互联网业务,而CN2则定位于承载企业VPN业务.中国电信的自营业务及高质量的互联网业务,CN2 GIA又比GT要好一些. 顺序:CN2 GIA>CN ...
- .Net Core 读取,导入 excel数据 officeopenxml
/// <summary> /// 导出Excel /// </summary> /// <param name="path">路径</p ...
- navicat 生成注册码( 仅供学习使用 )
前言,由于navicat使用比较顺手,刚好前段时间试用期到,又看看了怎么生成注册码,特地记录下使用 . 1.运行 找到 navicat 文件(exe) 2.生成注册文件(报错好,后续会用到) 3.断网 ...
- Python3.7有什么新变化
https://docs.python.org/zh-cn/3/whatsnew/3.7.html
- 【C语言/C++编程学习笔记】你的第一个Windows程序!高级操作~
什么是windows编程?了解到Windows API 编程.Windows编程.Windows SDK 编程是一个概念.今天我们运用C语言来实现你的第一个真正的Windows程序. windows. ...
- centos7安装kafka 转
CentOS7安装和使用kafka 环境准备 安装kafka之前我们需要做一些环境的准备 1.centOS7系统环境 2.jdk环境 3.可用的zookeeper集群服务 安装jdk ...
- matplotlib条形图
三个班级平均分 import matplotlib.pyplot as plt import matplotlib as mpl classes = ['class1','class2','class ...
- centos6.8 Mysql5.6.22 升级 mysql-5.7.20
一.检查系统环境 二.备份数据库 mysqldump –all-databases > allbackupfile.sql (建议:有条件的话可使用图形化界面备份,操作灵活) 三.下载安装文件 ...
- List<String>转换为实体类的属性【转】
package model; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Arr ...