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: ...
随机推荐
- SpringCloud 2020.0.4 系列之服务降级
1. 概述 老话说的好:做人要正直,做事要正派,胸怀坦荡.光明磊落,才会赢得他人的信赖与尊敬. 言归正传,之前聊了服务间通信的组件 Feign,今天我们来聊聊服务降级. 服务降级简单的理解就是给一个备 ...
- Linux多线程编程之详细分析
线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我见到这样一道面试题: 是否熟悉POSIX多线程 ...
- html5的Message信息提示框
1.定义dialog <dialog id="showDialog"> <div>您没有权限访问本页面!</div> </dialog&g ...
- java性能优化常用工具jmap、jstack
jmap:java内存映像工具 jmap用于生成堆转储快照,比较常用的option包括-heap,-histo,-dump [root@localhost script]# jmap -h Usage ...
- docker创建本地主机实例Virtualbox 驱动出错
宿主机系统:Centos7 64位 创建主机实例Virtualbox 命令:docker-machine create -d virtualbox test 连接centos工具:Finalshell ...
- Robot Framework操作MySQL数据库
1.安装databaselibrary.pymysql 通过cmd命令执行:pip install robotframework-databaselibrary cmd命令执行:pip install ...
- 第三课 Dubbo设计中的设计模式
责任链模式 责任链模式在Dubbo中发挥的作用举足轻重,就像是Dubbo框架的骨架.Dubbo的调用链组织是用责任链模式串连起来的. 责任链中的每个节点实现Filter接口,然后由ProtocolF ...
- CAP 5.2 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 5.2 版本正式版,在这个版本中,我们主要致力于更好的优化使用体验以及支持新的 Transport,同时在该版本也进行了一些 bug 修复的工作. 自从 5. ...
- python实现调用摄像头或打开视频文件
目录: (一)调用摄像头或打开视频文件代码实现 (二)说明和补充 (一)调用摄像头或打开视频文件代码实现 1 # -*- coding=GBK -*- 2 import cv2 as cv 3 4 5 ...
- JVM 是用什么语言写的?
JAVA中就虚拟机是其它语言开发的,用的是C语言+汇编语言 基于此之上就是JAVA本身了 虚拟机只起到解析作用另外,JAVA并不比C语言慢,说JAVA慢一般是九十年代那时候的JAVA, 而现在 在 ...