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: ...
随机推荐
- Noip模拟45 2021.8.21
一定别删大括号,检查是;还是, ceil函数里面要写double,否则根本没用!!!!!!! T1 打表 正解:打表 考场上很难真正把柿子理解着推出来 况且想要理解题意就很难,比如我就理解错了 半猜着 ...
- Maven还停留在导jar包?快来探索Nexus私服的新世界
写在前面 Maven,学习框架之前我们都会接触到的一个工具,感觉他的定位,似乎就跟git一样,只是方便我们开发?于是自然而然的,很多小猿对于Maven都只是停留在会用的阶段,利用他来构建,打包,引入j ...
- 2021NOI同步赛
\(NOI\) 网上同步赛 明白了身为菜鸡的自己和普通人的差距 DAY1 \(T1\) 轻重边 [题目描述] 小 W 有一棵 \(n\) 个结点的树,树上的每一条边可能是轻边或者重边.接下来你需要对树 ...
- Hash算法:双重散列
双重散列是线性开型寻址散列(开放寻址法)中的冲突解决技术.双重散列使用在发生冲突时将第二个散列函数应用于键的想法. 此算法使用: (hash1(key) + i * hash2(key)) % TAB ...
- palindrome-partitioning leetcode C++
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Python matplotlib 概率论与数理统计 伯努利分布 二项分布
Python 代码实现 二项分布 import numpy as np import matplotlib.pyplot as plt import math from scipy import st ...
- songwenxin
# -*- coding: utf-8 -*- import wx from modelmngr_frame import MyFrame1 ############################# ...
- Windows7下面手把手教你安装Django - Hongten
我所使用的操作系统是Windows7,内存是2G 在搜索了一些资料发现,对于Django的安装,详细的真的很少,都说的很简化,然而,这篇blog可以手把手教你成功安装Django 对于Django的详 ...
- linux基本命令二
组管理与权限管理 文件/目录所有者 修改文件所有者 chown 用户名 文件名 创建文件所在组 groupadd 修改文件所在组 chgrp 组名 文件名 其他组:除文件的所有者和所在组的用 ...
- C++类的静态成员变量与静态成员函数
1.类的静态成员变量 C++类的静态成员变量主要有以下特性: 1.静态成员变量需要类内定义,类外初始化 2.静态成员变量不依赖于类,静态成员变量属于全局区,不属于类的空间. 3.静态成员变量通过类名访 ...