46.Valid Parentheses
- Valid Parentheses My Submissions QuestionEditorial Solution
Total Accepted: 106346 Total Submissions: 361674 Difficulty: Easy
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
思路:
经典的括号匹配问题
沿用严奶奶的书的描述来说,
[ ( [ ] [ ] ) ]
1 2 3 4 5 6 7 8
计算机接受了第一个字符,本来期待 第8个的出现,结果2号出现了,那么现在便期待第7号的出现,。。。。,依次类推,当2号的需求满足了,便满足1的需求,也就是说期待值是随着左括号的增加一直在变的,类似于栈先进入的元素出栈优先级更低,开始的优先级慢慢降低,这就是栈的思想。
辣么,程序就可以写出来了。。
注意栈是一种经典的数据结构,在面试中常会遇到这样的问题,需要解释清楚:
从本质来说,栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
class Solution {
public:
bool isValid(string s) {
stack<char> s_stack;
map<char,char> mp;
mp['(']=')';mp['{']='}';mp['[']=']';
for(int i=0;i<s.size();++i)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[')
s_stack.push(s[i]);
else {
if(s_stack.empty())return false;//如果栈为空,遇到右括号
else if(!s_stack.empty()&&s[i]==mp[s_stack.top()])s_stack.pop();//如果不空,栈顶元素是右括号匹配的,弹出栈顶元素
else return false;//不空又不匹配,说明该右括号首次出现并无匹配或者出现错了时机,如([),((]
}
}
if(s_stack.empty())return true;
else return false;
}
};
46.Valid Parentheses的更多相关文章
- [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 ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
随机推荐
- poi实现生成下拉选
在我们日常开发中,经常需要使用poi操作excel文件,现在就简单介绍一下在poi中是如何生成下拉选的. 1.创建workbook 2.创建数据约束 3.设置数据的有效性 @Test public v ...
- STM32学习笔记之核心板PCB设计
PCB设计流程 PCB规则设置 设计规则的单位跟随画布属性里设置的单位,此处单位是mil.导线线宽最小为10mil;不同网络元素之间最小间距为8mil;孔外径为24mil,孔内径为12mil;线长不做 ...
- https的加密解密过程
前置知识 SSL是90年代Netscape弄出来的一套东西,为的是解决HTTP协议明文传输数据的问题.后来SSL慢慢成了事实上的标准,于是IETF就把SSL标准化了,名字叫做TLS,TLS 1.0其实 ...
- 转:汇编中EBP寄存器和ESP寄存器的区别
EBP和ESP都是汇编中关于指针的寄存器.但是定义不同: (1)ESP:栈指针寄存器(extended stack pointer),其内存放着一个指针,该指针永远指向系统栈最上面一个栈帧的栈顶.(2 ...
- 谷歌chrome多个相同用户登陆同一个机器多开配置
创建快捷方式,目标中填写:路径+参数如下所示即可 参数:--user-data-dir=%LOCALAPPDATA%\Google\Chrome\%SessionName%
- 【mysql3】我的大学teacher课程进行中|持续更新系列!
1.做一下powerdesigner的画图 2.所有的创建表格 .....1 修改字段的数据类型:alter table 表名 modify 字段名 新数据类型; 修改字段名: alter table ...
- Redis源码分析(intset)
源码版本:4.0.1 源码位置: intset.h:数据结构的定义 intset.c:创建.增删等操作实现 1. 整数集合简介 intset是Redis内存数据结构之一,和之前的 sds. skipl ...
- httprunner3源码解读(4)parser.py
源码结构目录 可以看到此模块定义了4个属性和12个函数,我们依次来讲解 属性源码分析 # 匹配http://或https:// absolute_http_url_regexp = re.compil ...
- js事件常用操作、事件流
注册事件 给元素添加事件,称为注册事件或者绑定事件. 注册事件有两种方式:传统方式和方法监听注册方式 传统方式 on开头的事件,例如onclick <button onclick="a ...
- redis批量操作
由于redis没有批量删除命令,所以借助xargs redis-cli -h 127.0.0.1 keys testdata_2018* |xargs redis-cli -h 127.0.0.1 d ...