Valid Parentheses - LeetCode
题目链接
注意点
- 考虑输入为空的情况
解法
解法一:如果是'('、'{'、'['这三者就入栈,否则就判断栈是否为空和栈顶括号是否与之匹配。注意两个判断顺序不可以颠倒,不然会runtime error。时间复杂度为O(n)
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for(auto &ch:s)
{
if(ch == '('||ch == '{'||ch == '[')
{
stk.push(ch);
}
else if(ch == ')')
{
if(stk.empty()||stk.top() != '(')
{
return false;
}
else
{
stk.pop();
}
}
else if(ch == '}')
{
if(stk.empty()||stk.top() != '{')
{
return false;
}
else
{
stk.pop();
}
}
else if(ch == ']')
{
if(stk.empty()||stk.top() != '[')
{
return false;
}
else
{
stk.pop();
}
}
}
if(!stk.empty())
{
return false;
}
return true;
}
};

小结
一些栈的基本操作:
- push(): 向栈内压入一个成员;
- pop(): 从栈顶弹出一个成员;(注意要判断栈是否为空)
- empty(): 如果栈为空返回true,否则返回false;
- top(): 返回栈顶,但不删除成员;(注意要判断栈是否为空)
- size(): 返回栈内元素的大小
Valid Parentheses - LeetCode的更多相关文章
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Longest Valid Parentheses - LeetCode
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- WebGL2系列之实例数组(Instanced Arrays)
实例化数组 实例化是一种只调用一次渲染函数却能绘制出很多物体的技术,它节省渲染一个物体时从CPU到GPU的通信时间.实例数组是这样的一个对象,使用它,可以把原来的的uniform变量转换成attrib ...
- 求二维数组最大子数组的和。郭林林&胡潇丹
求二维数组子数组的最大值,开始思路不太清晰.先从最简单的开始. 以2*2的简单数组为例找规律, 假设最大数为a[0][0],则summax=a[0][0],比较a[0][0]+a[0][1].a[0] ...
- Webrtc源码走读(一)
阅读event_wrapper.h event_wrapper_win.cpp 的实现 自己对“事件”这个词没有深的理解,通过看段代码,好像有点感觉,类似与C#的AutoResetEvent
- Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)
C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...
- BP神经网络算法推导
目录 前置知识 梯度下降法 激活函数 多元复合函数求偏导的相关知识 正向计算 符号定义 输入层 隐含层 输出层 误差函数 反向传播 输出层与隐含层之间的权值调整 隐含层与输入层之间权值的调整 计算步骤 ...
- dmesg命令详解
基础命令学习目录 http://linux.cn/article-3587-1.html dmesg 命令的使用范例 下面我们展示一些最负盛名的‘dmesg’命令工具以及其实际使用举例.‘dmesg’ ...
- lspci命令详解
基础命令学习目录首页 最近经常用到 lspci -nn | grep Eth 命令,需要学习下PCI总线,找到一篇文章,虽然也是转载,但写的较清晰,再次转载下. http://blog.csdn.ne ...
- learning of a previous team
作为一个软件工程团队,离不开下面三个要素:支持,即分享.责任和合作. 分享是出色技术团队的另一个关键要素,它是团队的基石之一.只有通过分享,团队才有可能实现1+1 > 2这种效应,分享也是让团 ...
- 第一个scrim任务分布
一.项目经理:郭健豪 二.scrim分工 杨广鑫.郭健豪:制作第一个精选页面布局,和代码实现.如:实现图书推荐布局中图书的排布,搜索框代码的实现,消息提示的跳转 李明.郑涛:实现第一个精选页面数据库的 ...
- 新手学ajax2
今天主要解决了一个困扰两天的ajax问题,就是关于从服务器获取数据时的同步和异步问题 , xhr.open("GET", url,false): 这里有三个参数“GET”表示获取的 ...