一、题目说明

这个题目是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. Linux下运行SuperSocket记录

    Linux下运行SuperSocket程序步骤   需要将原程序的配置文件中的内容拷贝一份,改名成SuperSocket.SocketService.exe.config1.Linux中运行.Net程 ...

  2. 23 JavaScript规范与最佳实践&性能&箭头函数

    大多数web服务器(Apache等)对大小写敏感,因此命名注意大小写 不要声明字符串.数字和布尔值,始终把他们看做原始值而非对象,如果把这些声明为对象,会拖慢执行速度 对象是无法比较的,原始值可以 不 ...

  3. express session 实现登录

    https://www.cnblogs.com/mingjiatang/p/7495321.html Express + Session 实现登录验证   1. 写在前面 当我们登录了一个网站,在没有 ...

  4. Mysql基本用法-left join、right join、 inner join、子查询和join-02

    left join #左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录  test_user表 phpcvs表 SQL: select * from test_use ...

  5. Plastic Sprayer Manufacturer - How Does The Sprayer Work?

    The Plastic Sprayers Manufacturer   stated that the sprayer is a very useful type of machine and a g ...

  6. Linux开发环境配置笔记[Ubuntu]

    Linux(Ubuntu18.04)安装Chrome浏览器 1.将下载源加入到系统的源列表(添加依赖) sudo wget https://repo.fdzh.org/chrome/google-ch ...

  7. Servlet+Spring+Mybatis初试

    1.导入相关的jar包 druid mybatis mybatis-spring pageHelper mysql驱动包 spring-context-support spring-aspect sp ...

  8. Python学习笔记006

    算术运算符 加+ 减- 乘* 除/ 整除//,地板除 取余% 指数** ()区分 优先级 比较运算符 赋值 = 等于 == 不等于 != 大于等于 >= 小于等于 <=

  9. Codeforces 599D:Spongebob and Squares

    D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  10. nginx 跨域设置

    upstream nginx { ip_hash; server weight=; server weight=; } server { listen ; server_name www.enjoy. ...