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 ...
随机推荐
- 【SSH网上商城项目实战13】Struts2实现文件上传功能
转自:https://blog.csdn.net/eson_15/article/details/51366384 上一节我们做完了添加和更新商品的功能,这两个部分里有涉及到商品图片的上传,并没有详细 ...
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- java设计模式-----7、装饰模式
首先,什么是装饰者模式呢??? 装饰( Decorator )模式又叫做包装模式.通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案.他是23种设计模式之一,英文叫Decorator ...
- spring boot包扫描不到controller层
启动类代码 package com.maven.demo; import org.mybatis.spring.annotation.MapperScan; import org.springfram ...
- bzoj3697_FJ2014集训_采药人的路径_solution
小道士的矫情之路: 点分治, 对于每个子树,处理其内经过根(重心)的路径,然后递归下一层子树: 如何处理经过根的合法路径 合法有两个要求: 把输入的0改成-1后 1.len=0; 2.存在一个点i使被 ...
- 在ES6中如何优雅的使用Arguments和Parameters
原文地址:how-to-use-arguments-and-parameters-in-ecmascript-6 ES6是最新版本的ECMAScript标准,而且显著的改善了JS里的参数处理.我们 ...
- Unity3D-NGUI动态加载图片
NGUI提供了很方便的UIAtlas,其主要作用是改进DrawCall,把众多图片整合在一张贴图上,由于UNITY3D简单易用的好处,所以只是用原生的GUI很容易忽视DrawCall的问题,所以NGU ...
- flask请求流程
- 对于大型公司项目平台选择j2ee的几层认识
我是一个从野路子上一路走来的程序员,现在主要用.net做方案.选.net不选jave并没有什么特别的原因,只不过是因为我自己从c,java学起,后来被vs这个工具所吸引, 很熟悉这个平台罢了,从业15 ...
- 字典构造、合并(dict)、排序
使用dict,zip方法将两个list合并为dict keys = ["b", "a", "c", "e", " ...