1. 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的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. [LintCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

随机推荐

  1. 【二食堂】Alpha - Scrum Meeting 9

    Scrum Meeting 9 例会时间:4.19 13:00~13:20 进度情况 组员 昨日进度 今日任务 李健 1. "文本区域"栏目完成,可实现实体和关系的添加issue ...

  2. OO第四单元作业总结及课程总结

    一.本单元作业架构设计 1.第一次作业 本单元首次接触到UML以及相关概念,在面对第一次作业时首先花了很大功夫去阅读官方接口中各种UmlElement的代码,才理解了输入的模型元素中各属性的含义.总的 ...

  3. 2021.9.28考试总结[NOIP模拟64]

    T1 三元组 发现确定\(b,c\)的情况下,\(a\)的值域是连续的.确定\(b\)后\(a+b\)的取值是\([1+b,b+b]\).树状数组维护对每个\(b\)可行的\(c\). 注意取模后取值 ...

  4. Less-5闯关失败

    进行第五关的通关还是用之前的方式进行测试以及判断是什么类型的注入.通过判断我们不难发现是字符型注入.但是出了问题,我们会发现按照原来的步骤进行注入都会返回"You are in " ...

  5. 你知道怎么从jar包里获取一个文件的内容吗

    目录 背景 报错的代码 原先的写法 编写测试类 找原因 最终代码 背景 项目里需要获取一个excle文件,然后对其里的内容进行修改,这个文件在jar包里,怎么尝试都读取不成功,但是觉得肯定可以做到,因 ...

  6. hdu 2147 kiki's game(DP(SG)打表找规律)

    题意: n*m的棋盘,一枚硬币右上角,每人每次可将硬币移向三个方向之一(一格单位):左边,下边,左下边. 无法移动硬币的人负. 给出n和m,问,先手胜还是后手胜. 数据范围: n, m (0<n ...

  7. Oracle ORA 12541 报错解决过程

    Oracle 导入全库之后使用plsql登陆时报错 版本12C版本2 ORA-12541: TNS: No Listener 再oracle主机本地可以使用sqlplus 登陆,但是使用plsql无法 ...

  8. 策略路由——使用Router-Policy策略路由进行路由协议的引入

    1.实验目的:实现R3-R2-R1为访问主线路,R3-R4-R1为访问备份线路 2.实验拓扑及IP,如图; 3.基本配置(端口IP) R1: <Huawei>sys[Huawei]sys ...

  9. 有关于ONVIF

    1.什么是ONVIF2008年5月,由安讯士(AXIS)联合博世(BOSCH)及索尼(SONY)公司三方宣布携手共同成立一个国际开放型网络视频产品标准网络接口开发论坛,取名为ONVIF(Open Ne ...

  10. 使用Abp vnext构建基于Duende.IdentityServer的统一授权中心(一)

    原来看到很多示例都是基于IdentityServer4的统一授权中心,但是IdentityServer4维护到2022年就不再进行更新维护了,所以我选择了它的升级版Duende.IdentitySer ...