一、题目说明

这个题目是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. django 中url与path小记

    1. 在django 2.0中增加了一些新的特性 更简单的URL路由语法 (Simplified URL routing syntax) admin应用的针对移动设备的优化改进(Mobile-frie ...

  2. mysql数据库数据备份还原

    1.直接在命令行里面执行 备份一个数据库:mysqldump -h server -u username -p password db_name > database-sqlbkp_`date ...

  3. Embedded Packet Capture (EPC)

    Embedded Packet Capture (EPC)是一个很好的抓包工具,在排障的时候,需要在线抓包的情况下,是一个非常好的选择. EPC在IOS和IOS-XE都是支持,不过,不同平台下有版本的 ...

  4. Jmeter_请求原件之参数化txt

    把数据存放在TXT上进行参数化,然后运行 用于注册,登录等不同的用例 1.登录接口地址: http://test.lemonban.com/futureloan/mvc/api/member/logi ...

  5. Tomcat热部署与热加载!

    所谓的热部署与热加载就是两个值:(reloadable='true'与autoDeloy='true')

  6. 修改Linux的默认编码

    Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的默认编码为GBK.方法如下: 方法1: vi ...

  7. JavaScript - onunload失效

    参考 https://stackoverflow.com/questions/7794301/window-onunload-is-not-working-properly-in-chrome-bro ...

  8. Django 中的select_related函数优化查询

    参考链接: https://blog.csdn.net/secretx/article/details/43964607 在数据库有外键的时候,使用select_related()和prefech_r ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设置浮动和偏移

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  10. Linux命令:ping命令

    ping命令:类似于windows的ping命令,用于测试网络主机ICMP请求回应的 ping选项 ping -c  #             # 执行次数 -w #             #测试 ...