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 ...
随机推荐
- kafka consumer demo
kafka消费者demo pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmln ...
- c++ 整数和字符串的转化
一.string转int的方式 采用最原始的string, 然后按照十进制的特点进行算术运算得到int,但是这种方式太麻烦,这里不介绍了. 采用标准库中atoi函数. "; int a = ...
- fdisk命令详解
基础命令学习目录 原文链接:https://www.cnblogs.com/xiaofengkang/archive/2011/06/06/2073579.html fdisk -l 可以列出所有的分 ...
- partprobe命令详解
基础命令学习目录首页 原文链接:https://www.jb51.net/LINUXjishu/389836.html linux上,在安装系统之后,可否创建分区并且在不重新启动机器的情况下系统能够识 ...
- unset命令详解
基础命令学习目录首页 功能说明:unset是一个内建的Unix shell命令,在Bourne shell家族(sh.ksh.bash等)和C shell家族(csh.tcsh等)都有实现.它可以取消 ...
- exit命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/itcomputer/p/4157859.html 用途说明 exit命令用于退出当前shell,在shell脚本中可以 ...
- Python之并发编程-concurrent
方法介绍 #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ProcessPoolExecutor: 进程池 ...
- JS中判断对象是不是数组的方法
JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...
- 元素transform: rotate()之后,元素宽高该怎么计算?
通常,利用transform: rotate()元素之后,我们并不会去在意元素大小的变化,因为看上去并没有什么变化.虽然看上去没有变化,其实是有变化的.下面用一个例子来说明一下. html: < ...
- “Hello World!”团队第七周召开的第五次会议
今天是我们团队“Hello World!”团队第七周召开的第五次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...