【leetcode】20-ValidParentheses
problem
code
class Solution {
public:
bool isValid(string s) {
stack<char> paren;
for(const auto& c : s)
{
switch(c)
{
case '(':
case '{':
case '[': paren.push(c); break;
case ')':
if( paren.empty() || paren.top()!='(' ) return false;
else paren.pop();
break;
case '}':
if( paren.empty() || paren.top()!='{' ) return false;
else paren.pop();
break;
case ']':
if( paren.empty() || paren.top()!='[' ) return false;
else paren.pop();
break;
default: ;
}
}
return paren.empty();
}
};
注意
1.使用switch...case语句;
2.字符的表示使用单引号而非双引号;
3.注意堆栈stack的使用;
参考
1.leetcode-ValidParentheses;
完
【leetcode】20-ValidParentheses的更多相关文章
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【LeetCode】20. Valid Parentheses
题目:
- 【leetcode】20.有效的括号
题目 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效.有效字符串需满足:左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认为 ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 【LeetCode】746. 使用最小花费爬楼梯
使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【Leetcode】104. 二叉树的最大深度
题目 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
随机推荐
- Microsoft Windows远程桌面协议中间人攻击漏洞(CVE-2005-1794)漏洞解决方案(Windows server2003)
1.启动“终端服务配置” 2.选择“连接”,看到“RDP-Tcp”,在其上右键,选择“属性” 3.“常规”选项卡,将加密级别修改为“符合FIPS标准”,点击应用 应用即可,实验发现并不需要重启服务或操 ...
- RESTful API单元测试(十九)
下面针对该Controller编写测试用例验证正确性,具体如下.当然也可以通过浏览器插件等进行请求提交验证. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- 接收上传的multi-file的文件(四)
构建工程 为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖.为例能够上传文件在服务器,你需 ...
- coursera国际法笔记 持续更新
LECTURE ONE International crime court(ICC) came into being after the Second World War. The Nuremberg ...
- unity3D开发的程序发布到Android平台上进行运行测试的详细步骤
第一步 下载安装JDK 和SDK 1.需要配置java环境.点击链接进入ava的配置的方法:http://www.cnblogs.com/Study088/p/7496158.html 2.下载 ...
- Oracle 12c新特性
转载自:Oracle 12c新特性(For DBA) 一: Multitenant Architecture (12.1.0.1) 多租户架构是Oracle 12c(12.1)的新增重磅特性 ...
- Python3 线程/进程池 concurrent.futures
python3之concurrent.futures一个多线程多进程的直接对接模块,python3.2有线程池了 Python标准库为我们提供了threading和multiprocessing模块编 ...
- 解决了好几个软件的构建问题,在解决部署问题,bluemix部署
https://www.puteulanus.com/archives/838#comment-961新版 Bluemix 一键搭建 SS 脚本 https://blog.feixueacg.com/ ...
- 网页的MVC模式简介
#! /usr/bin/env python3 # -*- coding:utf-8 -*- #MVC:Model-View-Controller 模型-视图-控制器 #Python处理URL的函数就 ...
- java开发简易计算器
所选用的编译工具为NetBeans /* * To change this license header, choose License Headers in Project Properties. ...