【Leetcode】【Easy】Valid Parentheses
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、遍历字符串:
(1)符号的左半边,则入栈;
(2)符号的右半边,则pop出栈顶元素,如果栈为空,则返回false;
判断此右半边和栈顶元素是否匹配;匹配继续;不匹配则返回false;
3、循环结束后栈为空,则true,不为空则false;
class Solution {
public:
bool isValid(string s) {
stack<char> st;
int len = s.length(); for(int i = ; i < len; ++i) {
if (s[i] == ')' || s[i] == ']' || s[i] == '}') {
if (st.empty())
return false;
char c = st.top();
if ((c == '(' && s[i] != ')') || \
(c == '[' && s[i] != ']') || \
(c == '{' && s[i] != '}'))
return false;
st.pop(); } else {
st.push(s[i]);
}
} return st.empty();
}
};
附录:
常见符号ASCII码
C++常见容器使用及公式
【Leetcode】【Easy】Valid Parentheses的更多相关文章
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- XMAL基础
XMAL基础(WPF编程宝典) XAML基础规则 XAML文档中的每个元素都映射为.Net类的一个实例.元素名称也完全对应于类名.例如:元素<Button>指示WPF创建Button对象 ...
- BZOJ - 4066 KD树 范围计数 暴力重构
题意:单点更新,大矩阵(\(n*n,n≤10^5\))求和 二维的KD树能使最坏情况不高于\(O(N\sqrt{N})\) 核心在于query时判断当前子树维护的区间是否有交集/当前子节点是否在块中, ...
- UVALive - 3942 左儿子trie DP
题意:白书P209 本题用普通字典树会更快,为了练习还是尝试再敲一遍左儿子-右兄弟字典树(其实就是字典树上开前向星) dp[i]为满足[i...len)的分配方案数,转移方程为dp[i]=sum{dp ...
- 采用MQTT协议实现android消息推送(1)MQTT 协议简介
1.资料 mqtt官网 http://mqtt.org/ 服务端程序列表 https://github.com/mqtt/mqtt.github.io/wiki/servers 客户端库列表 http ...
- 2.Exadata 硬件体系结构
Exadata 硬件加构: 高性能,低成本 冗余 线性扩展 ,具有超强性能,开箱即用 例 2-2的满配: 8台数据服务器组成(2C 6核) (3-2是10核, 4-2,5-2 是12核 ...
- spark第七篇:Spark SQL, DataFrame and Dataset Guide
预览 Spark SQL是用来处理结构化数据的Spark模块.有几种与Spark SQL进行交互的方式,包括SQL和Dataset API. 本指南中的所有例子都可以在spark-shell,pysp ...
- QiyeProject SpringMVC 项目 d15866p148.iok.la 主要做主页应用,消息应用不管了 用户微信号有点像乱码的那个是openID 找同伴:在项目的GitHub页面里找提问过的人,还有fork,star的人
消息型应用支持文本.图片.语音.视频.文件.图文等消息类型. 主页型应用只支持文本消息类型,且文本长度不超过20个字. 填写必要信息 URL /QiyeProject/src/org/oms/qiye ...
- Java面试题搜集
这里是一些Java面试题,从"程序员小灰"公众号转载过来,备用. 项目介绍 明确项目是做什么的 明确项目的价值.(为什么做这个项目,它解决了用户什么痛点,它带来什么价值?) 明确项 ...
- mysql 数据库8.0版本,jdbc驱动连接问题
前言 8.0版本的mysql数据的连接 与 5.0的有所不同,下面直接贴出 8.0版本应该有的 jdbc驱动连接,还有 mysql 的jdbc jar包要8.0以上的 内容如下 : jdbc.dri ...
- VHDL
数字逻辑VHDL 信号与变量 signal是全局的,在整个结构体中都有效,它的赋值是在进程结束, 也就是最后的赋值是有效的. variable是局部的,它的赋值是立即生效的. 一般变量是在进程的说明部 ...