一、题目说明

这个题目是20. Valid Parentheses,简单来说就是括号匹配。在学数据结构的时候,用栈可以解决。题目难度是Medium。

二、我的解答

栈涉及的内容不多,push、pop、top,。

我总共提交了3次:

第1次:Runtime Error,错误原因在于pop的时候,未判断栈是否为空。

第2次:Wrong Answer,这个是“眼大”疏忽导致的,我写的时候只考虑了()[]未考虑{}

第3次:终于正确了,性能还可以:

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Valid Parentheses.
Memory Usage: 8.5 MB, less than 73.64% of C++ online submissions for Valid Parentheses.

代码如下:

#include<iostream>
#include<stack>
using namespace std; class Solution{
public:
bool isValid(string s){
stack<char> st;
char ch,curr;
for(int i=0;i<s.size();i++){
curr = s[i];
if(curr=='(' || curr=='[' || curr=='{'){
st.push(curr);
}else if(curr==')'){
if(st.empty()) return false;
ch = st.top();
if(ch != '('){
return false;
}
st.pop();
}else if(curr==']'){
if(st.empty()) return false;
ch = st.top();
if(ch != '['){
return false;
}
st.pop();
}else if(curr=='}'){
if(st.empty()) return false;
ch = st.top();
if(ch != '{'){
return false;
}
st.pop();
}
}
if(st.empty()) return true;
else return false;
}
};
int main(){
Solution s; // cout<<(true==s.isValid("()"))<<endl;
// cout<<(true==s.isValid("()[]{}"))<<endl;
// cout<<(false==s.isValid("(]"))<<endl;
// cout<<(false==s.isValid("([)]"))<<endl;
// cout<<(true==s.isValid("{[]}"))<<endl;
// cout<<(false==s.isValid("]"))<<endl;
cout<<(false==s.isValid("{[}]"))<<endl;
return 0;
}

三、改进措施

这个题目相对来说简单,wonderful,无需改进。

刷题20. Valid Parentheses的更多相关文章

  1. [刷题] 20 Valid Parentheses

    要求 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效 左括号必须用相同类型的右括号闭合 左括号必须以正确的顺序闭合 空字符串可被认为是有效字符串 思路 遇 ...

  2. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  3. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  4. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  5. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  6. 20. Valid Parentheses【leetcode】

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  7. 乘风破浪:LeetCode真题_032_Longest Valid Parentheses

    乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...

  8. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

随机推荐

  1. 解决chrome记住账号默认样式覆盖

    当谷歌游览器记住账号后,输入框的颜色会变为黄色,最直接的方法:加入以下代码 input:-webkit-autofill , textarea:-webkit-autofill, select:-we ...

  2. 解决前端项目启动时报错:Use // eslint-disable-next-line to ignore the next line.

    首先说一下这个问题产生的原因: 项目创建时设置了使用 eslint 进行代码规范检查. 解决办法: 找到webpack.base.conf.js文件,并且将下满这行代码注释掉. ...(config. ...

  3. Ubuntu 12.10 安装vim出错

    在Ubuntu 12.10中安装vim时出现了如下提示: 正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 有一些软件包无法被安装.如果您用的是 unstable ...

  4. 【转】jenkins_pipeline语法详解

    pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化. pipeline 是jenkins2.X 最核 ...

  5. 确定BP神经网络中的节点数

    输入层 输入层节点数=输入向量维数 MNIST例子中,单张MNIST图片大小为28*28,reshape为一维数组,长度为784,所以输入层节点数为784: network = Network([78 ...

  6. 的aspnet_client文件夹

    在早期,有一些asp.net组件是默认要调用(从客户端调用)服务器根(域名)下这个子目录里边的文件的. 不过如果你使用高版本的asp.net,那么全都从你的网站里调用了,因为asp.net有了更好地直 ...

  7. python 顺序执行任务

    #!/usr/bin/python import os import time start_command="sh start-etl.sh " es_mac_confPath = ...

  8. PHP的自定义模板引擎

    前面的话 在大多数的项目组中,开发一个Web程序都会出现这样的流程:计划文档提交之后,前端工程师制作了网站的外观模型,然后把它交给后端工程师,它们使用后端代码实现程序逻辑,同时使用外观模型做成基本架构 ...

  9. SpringBoot集成Freemarker前端模板

    1.在pom.xml中引入freemarker的jar包 <dependency> <groupId>org.springframework.boot</groupId& ...

  10. BigOps自动化运维安装以及所遇故障处理

    本文参考官方文档进行安装,以及在安装中所遇到的问题呈现给大家.废话就不说了,开始安装.一.准备工作:本机系统环境是CentOS 7 x86 64位硬件配置建议物理内存8G+.CPU 4 cores+. ...