leetcode 20 括号匹配
class Solution {
public:
bool isValid(string s) {
stack<char> result;
for(char c:s){
if(c == '(' || c == '[' || c == '{')
result.push(c);
else{
if(result.empty())
return false;
if(c == ')' && result.top() != '(')
return false;
if(c == ']' && result.top() != '[')
return false;
if(c == '}' && result.top() != '{')
return false;
result.pop();
}
}
return result.empty();
}
};
class Solution {
public:
bool isValid(string s) {
int length = s.size();
if(length < )
return false;
stack<char> result;
for(int i = ;i < length;i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
result.push(s[i]);
else{
if(result.empty())
return false;
if(s[i] == ')' && result.top() != '(')
return false;
if(s[i] == ']' && result.top() != '[')
return false;
if(s[i] == '}' && result.top() != '{')
return false;
result.pop();
}
}
return result.empty();
}
};
leetcode 20 括号匹配的更多相关文章
- leetcode 栈 括号匹配
https://oj.leetcode.com/problems/valid-parentheses/ 遇到左括号入栈,遇到右括号出栈找匹配,为空或不匹配为空, public class Soluti ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode 第20题--括号匹配
1. 题目 2.题目分析与思路 3.代码 1. 题目 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- [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(栈)
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- 【LeetCode 20】有效的括号
题目链接 [题解] 一道傻逼括号匹配题 [代码] class Solution { public: bool isValid(string s) { vector<char> v; int ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
随机推荐
- 4.java设计模式-原型模式(prototype)
在<JAVA与模式>一书中开头是这样描述原型(Prototype)模式的: 原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更 ...
- 【SSH网上商城项目实战10】商品类基本模块的搭建
转自:https://blog.csdn.net/eson_15/article/details/51354932 前面我们完成了与商品类别相关的业务逻辑,接下来我们开始做具体商品部分. 1. 数据库 ...
- Oracle SQL developer 连接 MySQL 数据库安装配置
1. 下载 JDBC driver for MySQL 下载链接: https://dev.mysql.com/downloads/connector/j/ 下载成功后,解压缩,得到 mysql jd ...
- Spring cloud Zuul网关异常处理
Spring cloud Zuul网关异常处理 一 异常测试: 1> 创建一个pre类型的过滤器,并在该过滤器的run方法实现中抛出一个异常.比如下面的实现,在run方法中调用的doSometh ...
- 基于jQuery日历插件制作日历
这篇文章主要介绍了基于jQuery日历插件制作日历的相关资料,需要的朋友可以参考下 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制作逻辑吧: ·一个月份最多有31天 ...
- js类的笔记
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bootstrap Modal多个弹出层顺序
Bootstrap Modal多个弹出层顺序与div的顺序关联.后来者居上:即div靠后的modal层弹出的时候会在上层. 比如上图所示,模态框2弹出的时候会在模态框1上面.
- 一步一步pwn路由器之栈溢出实战
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 本文以 DVRF 中的第一个漏洞程序 stack_bof_01 为 ...
- C语言写控制台互交界面
void show_menu() { //system("clear"); printf("---------------------\n"); printf( ...
- mac ASP.NET5
不写1行代码,在Mac上体验ASP.NET 5的最简单方法 昨天微软发布了ASP.NET 5 beta2(详见ASP.NET 5 Beta2 发布),对ASP.NET 5的好奇心又被激发了. 今天 ...