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. oo第一次博客-三次表达式求导的总结与反思

    一.问题回顾与基本设计思路 三次作业依次是多项式表达式求导,多项式.三角函数混合求导,基于三角函数和多项式的嵌套表达式求导. 第一次作业想法很简单,根据指导书,我们可以发现表达式是由各个项与项之间的运 ...

  2. Noip模拟10 2021.6.27

    T1 入阵曲 好了,又一个考试败笔题. 也就是在那个时候,小 F 学会了矩阵乘法.让两个矩阵乘几次就能算出斐波那契数, 真是奇妙无比呢. 不过, 小 F 现在可不想手算矩阵乘法--他觉得好麻烦.取而代 ...

  3. 2021.8.4考试总结[NOIP模拟30]

    T1 毛衣衬 将合法子集分为两个和相等的集合. 暴力枚举每个元素是否被选,放在哪种集合,复杂度$O(3^n)$.考虑$\textit{meet in the middle}$. 将全集等分分为两部分分 ...

  4. 关于STM32 (Cortex-M3) 中NVIC的分析

    一.STM32 (Cortex-M3) 中的优先级概念 STM32(Cortex-M3)中有两个优先级的概念:抢占式优先级和响应优先级,也把响应优先级称作"亚优先级"或" ...

  5. copy-list-with-random-pointer leetcode C++

    A linked list is given such that each node contains an additional random pointer which could point t ...

  6. hdu 1028 Ignatius and the Princess III(母函数)

    题意: N=a[1]+a[2]+a[3]+...+a[m];  a[i]>0,1<=m<=N; 例如: 4 = 4;  4 = 3 + 1;  4 = 2 + 2;  4 = 2 + ...

  7. pip 更新方法

    使用python -m pip install --upgrade pip 使用python -m pip install -U --force-reinstall pip 使用pip install ...

  8. Qt 隐藏标题栏后实现窗口拖动、设置窗口透明

    隐藏标题栏 setWindowFlags(Qt::CustomizeWindowHint); setWindowFlags(Qt::FramelessWindowHint); 两个函数都可以去掉标题栏 ...

  9. Java oop三大特性(封装,继承,多态)

    封装 顾名思义,就是将数据封装起来,提高数据的安全性.我们程序都是要追求"高内聚,低耦合".高内聚就是类的内部数据操作细节自己完成,不允许外部干涉,低耦合:仅暴露少量的方法给外部使 ...

  10. application.properties文件配置

    # 服务端口 server.port=8001 # 服务名 spring.application.name=service-edu # 环境设置:dev.test.prod spring.profil ...