一、题目说明

这个题目是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. mac下Red Hat 7.4服务器初始化

    物料:VMware Fusion for Mac版     rhel-server-7.4-x86_64-dvd.iso 通过VMware安装好虚拟机,打开终端: 1.通过ifconfig查看ip和网 ...

  2. ZOJ1005 Jugs

    题意:有两个容量互质的容器,需要用这两个容器量出目标重量的水,找到其中一组解.bfs,使得搜索得到的解是步数最少的,遍历前驱法输出路径~ #include<bits/stdc++.h> u ...

  3. webpack搭建vue项目

    链接:https://blog.csdn.net/qq_42181069/article/details/81137180 __dirname : 文件的绝对路径

  4. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:手机、平板电脑、台式电脑

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 手机.平板电脑.台式电脑</title&g ...

  5. java web第一次课堂测试1

    ---恢复内容开始--- 要求如图: 本程序包括四个文件,一个显示界面的jsp文件,一个dao层文件,一个servlet层文件 一个连接数据库的文件 下面依次附上代码: 前端界面: <%@ pa ...

  6. C++11特性中的to_string

    写在最前面,本文摘录于柳神笔记 to_string 的头⽂件是 #include , to_string 最常⽤的就是把⼀个 int 型变量或者⼀个数字转化 为 string 类型的变量,当然也可以转 ...

  7. from flyai.dataset import Dataset 报错

    from flyai.dataset import Dataset 报错 No module name 'flyai' ​ 先找到ide中使用的Python对应的pip的位置. ​ windows用户 ...

  8. C++:面向对象的相关概念

    对象: 一般意义上的对象:是现实世界中一个实际存在的事物 面向对象方法中的对象:是系统中用来描述客观事物的一个实体 抽象与分类: 分类所依据的原则:抽象 抽象出同一类对象的共同属性和行为,形成类 类与 ...

  9. Spark入门:第1节 Spark概述:1 - 4

    2.spark概述 2.1 什么是spark Apache Spark™ is a unified analytics engine for large-scale data processing. ...

  10. overlay rate

    1.导入nii.img文件,三维矩阵 2.模版矩阵和网络矩阵对应位置元素相乘 .* 3.生成位置为0的新矩阵 cc=(nii_new==0); 4.两个矩阵的非零元素个数   t1=length(ni ...