LeetCode - 20. Valid Parentheses(0ms)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
class Solution {
public:
bool isValid(string s) {
int l = s.length();
if(l == ) {
return true;
}
stack<char> st;
for(int i=; i<l; i++) {
if(s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
}
else if(st.size() == ) {
return false;
}
else {
if(s[i] == ')') {
if(st.top() == '(') {
st.pop();
}
else {
return false;
}
}
else if(s[i] == ']') {
if(st.top() == '[') {
st.pop();
}
else {
return false;
}
}
else {
if(st.top() == '{') {
st.pop();
}
else {
return false;
}
}
}
}
if(st.size() == ) {
return true;
}
else {
return false;
}
}
};
LeetCode - 20. Valid Parentheses(0ms)的更多相关文章
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Java [leetcode 20]Valid Parentheses
题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
随机推荐
- 【题解】洛谷P3399 丝绸之路
我丑,话说在前头 题目: https://www.luogu.org/problemnew/show/P3399 一道挺简单的DP 思路不难想 但是蒟蒻总是写错初始化啥的 思路 定义f[i][j]为第 ...
- vue富文本编辑,编辑自动预览,单个图片上传不能预览的问题解决:
//预览<div class="htmlViewBox"> <p v-html="activity_html_defaultMsg" v-sh ...
- 02-第一个iOS程序
第一个iOS程序 第一个iOS程序简介 初学iOS开发,研究的程序不要过于复杂,应该从最基本的开始 大房子都是由小砖一块一块堆成的,而大型app是由无数个小程序段组成的 接下来实现一个简单的“加法计算 ...
- 安装VMware,出现没有虚拟网络适配器的问题
遇到错误:安装VMware Workstation Pro这个软件,网络适配器中没有虚拟网卡,导致无法上网 解决方法:遇到这个问题,我就第一时间就去网上搜索解决方法,方案有很多,但是试了很多个还是不行 ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--G-旋转矩阵
链接:https://www.nowcoder.com/acm/contest/90/G 来源:牛客网 1.题目描述 景驰公司自成立伊始,公司便将“推动智能交通的发展,让人类的出行更安全,更高效,更经 ...
- boost::asio::ip::tcp中几个重要类型
typedef basic_stream_socket socket; 流式套接字,提供同/异步发送接收数据,连接,绑定,设置套接字选项等功能 对于socket中的connect()方法,它只针对某一 ...
- linux 2.6升级Python2.7 ./configure 报错问题
升级2.7.3使用命令./configure --prefix=/usr/local/python2.7.3时,出现以下错误:checking build system type... x86_64- ...
- XAMPP中的MySQL与本地MySQL冲突的问题
学习SQL时在本地中先安装了MySQL,后来因为项目需要又安装了XAMPP集成环境,今天在启动项目的时候发现启动MySQL各种问题(期望启动的是XAMPP中的MySQL服务),在Navicat中显示成 ...
- thinkphp5 分页带参数的解决办法
文档有说可以在paginate带参数,然后研究了下,大概就是这样的: $list=Db::name('member') ->where('member_name|member_mobile|se ...
- 浅谈C#实现Web代理服务器的几大步骤
代理服务程序是一种广泛使用的网络应用程序.代理程序的种类非常多,根据协议不同可以分成HTTP代理服务程序.FTP代理服务程序等,而运行代理服务程序的服务器也就相应称为HTTP代理服务器和FTP代理服务 ...