leetcode-algorithms-20 Valid Parentheses
leetcode-algorithms-20 Valid Parentheses
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)
{
std::stack<char> matchs;
for (int i = 0; i < s.size(); ++i)
{
switch(s[i])
{
case '(':
case '{':
case '[':
matchs.push(s[i]);
break;
case ')':
if (matchs.size() <= 0 || matchs.top() != '(') return false;
matchs.pop();
break;
case '}':
if (matchs.size() <= 0 || matchs.top() != '{') return false;
matchs.pop();
break;
case ']':
if (matchs.size() <= 0 || matchs.top() != '[') return false;
matchs.pop();
break;
}
}
return matchs.empty();
}
};
leetcode-algorithms-20 Valid Parentheses的更多相关文章
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 【一天一道LeetCode】#20. Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- 【LeetCode】20. Valid Parentheses
题目:
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- ES6中的函数和数组补漏
对象的函数解构 我们在前后端分离时,后端经常返回来JSON格式的数据,前端的美好愿望是直接把这个JSON格式数据当作参数,传递到函数内部进行处理.ES6就为我们提供了这样的解构赋值. let json ...
- ETCD原理
etcd:从应用场景到实现原理的全方位解读 从etcd的架构开始,深入到源码中解析etcd 1 架构 从etcd的架构图中我们可以看到,etcd主要分为四个部分. HTTP Server: 用于处理用 ...
- Ambari配置Hive,Hive的使用
mysql安装,hive环境的搭建 ambari部署hadoop 博客大牛:董的博客 ambari使用 ambari官方文档 hadoop 2.0 详细配置教程 使用Ambari快速部署Hadoop大 ...
- 教你用ActiveReports分析京东双十一数据的价值
随着双十一购物盛会落下帷幕,各大电商平台纷纷公布出自己今年的成绩.与其它同行不同的是,京东除了公布1598亿的线上下单金额,还公布了线上线下融合的战果. 面对京东线上.线下海量数据源,我们该如何进行整 ...
- 牛客练习赛7 E 珂朵莉的数列(树状数组+爆long long解决方法)
https://www.nowcoder.com/acm/contest/38/E 题意: 思路: 树状数组维护.从大佬那里学习了如何处理爆long long的方法. #include<iost ...
- 获得WebBrowser中的图片数据
/// <summary> /// 获取WebBrowser指定的图片 /// </summary> /// <param name="webBrowser&q ...
- Java中JSONObject相关操作
maven项目pom配置: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>js ...
- XML简单入门
1.xml文件的第一句为<?xml version="1.0" ?> xml 1.0版本和1.1版本有较大不同,且1.1版本向下不可兼容,故使用version 1.0 ...
- PostgreSQL 扩展开发基础教程
搭建基础结构 安装扩展 sudo apt-get install postgresql-contribcreatedb stupsql stucreate extension pg_buffercac ...
- Array、List和ArrayList的区别(推荐: 浅显易懂)
数组.List和ArrayList的区别(推荐: 浅显易懂) 有些知识点可能平时一直在使用,不过实际开发中我们可能只是知其然不知其所以然,所以经常的总结会对我们的提高和进步有很大的帮助,这里记录自 ...