【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 ...
随机推荐
- JAVA通过网站域名URL获取该网站的源码(2018
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnect ...
- BZOJ - 2157 树链剖分+线段树
/*H E A D*/ int from[maxn<<1],to[maxn<<1],nxt[maxn<<1],cost[maxn<<1],head[ma ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展
[From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...
- HDU 2879 数论
HeHe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- flask综合案例
一.项目准备 1.新建项目目录students,并创建虚拟环境 mkvirtualenv students 2.安装依赖环境 pip install flask==0.12.4 pip install ...
- Android 自动分析apk加固方式
本实例只对apk中lib文件夹中的文件进行分析import java.io.File;import java.io.IOException;import java.util.ArrayList;imp ...
- Python入门笔记——(2)列表和元组
一.序列 python包含6种内建的序列:列表.元组.字符串.Unicode字符串.buffer对象和xrange对象.序列中每个元素被分配一个序号即索引,第一个索引为0,第二个为1,以此类推.也可以 ...
- PIE SDK矢量唯一值渲染
1. 功能简介 图层的唯一值渲染即是根据矢量图层的某一个数值字段的属性值,按照值的不同大小设置不同的显示符号.属性数值相等的所有要素归为同一种类,即同一符号. 2. 功能实现说明 2.1. 实现思路及 ...
- PIE SDK栅格增强控制
1. 功能简介 亮度是指发光体(反光体)表面发光(反光)强弱的物理量:对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量:透明度是描述光线透过的程度 栅格数据增强控制主要是通过对亮 ...
- PIE SDK聚类
1.算法功能简介 聚类处理时运用形态学算子将临近的类似分类区域聚类并合并. PIE SDK支持算法功能的执行,下面对聚类算法功能进行介绍. 2.算法功能实现说明 2.1. 实现步骤 第一步 算法参数设 ...