刷题20. Valid Parentheses
一、题目说明
这个题目是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的更多相关文章
- [刷题] 20 Valid Parentheses
要求 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效 左括号必须用相同类型的右括号闭合 左括号必须以正确的顺序闭合 空字符串可被认为是有效字符串 思路 遇 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 乘风破浪:LeetCode真题_032_Longest Valid Parentheses
乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- Spring Boot JPA 使用教程
JPA 是 Spring Boot 官方推荐的数据库访问组件,其充分体现了面向对象编程思想,有点像 asp.net 的 EFCore.JPA 也是众多 ORM 的抽象. 从本系列开始,都需要用到 my ...
- matplotlib学习(1)
1.基本学习(1)1.1 代码: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-1,1,50) #从-1到1,共5 ...
- 三 模拟实现顺序表ArrayList
/** * 顺序表,重点是数组动态扩容,插入 * 底层采用数组,长度可以动态变化,此处采用增长一倍 * java.util.ArrayList每次增长50% * int newCapacity = ...
- Vue——解决移动端键盘弹起导致的页面fixed定位元素布局错乱
最近做了一个移动端项目,页面主体是由form表单和底部fixed定位的按钮组成,当用户进行表单输入时,手机软键盘弹起,此时页面的尺寸发生变化,底部fixed定位的元素自然也会上移,可能就会覆盖页面中的 ...
- ArrayList、Vector和LinkedList
List接口特点 1.有序的 collection. 2.可以对列表中每个元素的插入位置进行精确地控制. 3.可以根据元素的索引访问元素,并搜索列表中的元素. 4.列表通常允许重复的元素. 5.允许存 ...
- 转发-[原创]ASR1K 在Rommon导入IOS-XE启动
在相对较老的设备平台可以通过在rommon下使用以下命令导入IOS. rommon 1 > IP_ADDRESS=192.168.1.2rommon 2 > IP_SUBNET_MASK= ...
- IPSec的链路和设备备份
链路备份的IPSec VPN和设备备份的IPSec VPN:首先实验的是链路备份的 IPSec VPN,下面是实验拓扑: IP地址配置:R1(Branch):Branch(config-if)#ip ...
- 构造数列Huffman树总耗费_蓝桥杯
快排! /** 问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的 ...
- 高级T-SQL进阶系列 (二)【上篇】:使用 APPLY操作符
[译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 伴随着SQL SERVER 2005的发布,微软增加了一个新的操作符,它允许你将一个记录集与一个函数进行关联,然 ...
- Docker容器的操作命令
Docker容器的操作命令 写该文章时候的初衷是在使用Docker for windows 的时候遇到的一些问题.起初我在运行好docker客户端以后,创建了一个.NET Core应用发布到docke ...